여러가지/Docker & AWS

[정리] 환경 변수 및 환경 변수 파일

15June 2024. 6. 5. 10:53

ㄱ. 환경 변수

(파일) Dockerfile

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

FROM node:latest

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

ENV PORT 3001    #  환경 변수

EXPOSE $PORT

CMD ["npm", "start"] 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

(파일) server.js

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

const http = require('http');

const hostname = '0.0.0.0';
const port = process.env.PORT || 3001;    # 환경 변수

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello Nodemon World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

ㄴ. 환경 변수 파일

(파일) test.env

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

(파일) Dockerfile

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

FROM node:14
ENV DB_HOST=${DB_HOST}
ENV DB_USER=${DB_USER}
ENV DB_PASSWORD=${DB_PASSWORD}   # Dockerfile에서 환경 변수 파일 사용

COPY . /app
WORKDIR /app
RUN npm install
CMD ["npm", "start"]

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

 

# docker run -d --name my-container  -e DB_USER=root -e DB_PASSWORD=password my-image

※ -e 옵션을 사용하여 컨테이너 실행 시 환경 변수 값 변경 가능합니다.

(사용 시기) 컨테이너 실행 시 혹은 어플리케이션 실행 도중

 

(파일) docker-compose.yml

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

version: '3'
services:
  db:
    image: mysql:latest
    env_file:
      - .env   # 환경 변수 파일 불러오기
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}    # Docker Compose에서 환경 변수 파일 사용
  web:
    image: your-web-app:latest
    env_file:
      - .env
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------