도커 파일 예시
FROM node:latest
COPY package.json package-lock.json .prettierrc ./
RUN npm install
COPY src ./src
COPY assets ./assets
EXPOSE 3000
ENTRYPOINT [ "npm", "start" ]
Dockerignore 예시
node_modules
.idea
.git
.gitignore
.dockerignore
Dockerfile
*.md
*.sh
*.yml
scripts
docker-compose.yml 예시
services:
server:
build: . # 현재 디렉토리에서 Dockerfile을 빌드
ports:
- '3000:3000' # 호스트와 컨테이너의 포트 매핑
environment:
- PORT=
- HOST=
- CLIENT_VERSION=
- DB1_NAME=
- DB1_USER=
- DB1_PASSWORD=
- DB1_HOST=
- DB1_PORT=
- DB2_NAME=
- DB2_USER=
- DB2_PASSWORD=
- DB2_HOST=
- DB2_PORT=
- REDIS_HOST=
- REDIS_PORT=
- REDIS_PASSWORD=
restart: always # 컨테이너가 종료되면 자동으로 다시 시작
도커 이미지 생성
# docker build -t [이미지명]:[태그] [Dockerfile 위치]
# 태그는 생략 가능, 이미지명은 소문자만 가능
# Dockerfile이 위치한 디렉토리에서 실행할 경우
docker build -t moonrabbits .
3. docker-compose 실행 명령어(EC2 ubuntu로 배포 시 sudo를 앞에 붙이기)
1) 컨테이너 빌드 및 실행
docker-compose up -d
-d: 백그라운드 모드에서 실행 (디버깅을 위해 생략 가능)
2) 실행 중인 컨테이너 확인
docker-compose ps
3) 컨테이너 로그 확인
docker-compose logs -f
4) 컨테이너 중지
docker-compose down
- 컨테이너를 중지하고 삭제함.
5) 컨테이너 재시작
docker-compose restart
Dockerfile 변경 후 컨테이너 다시 실행
📌 수정이 끝났으면 기존 컨테이너를 삭제하고 다시 실행해야 변경 사항이 반영됨.
docker-compose down
docker-compose up --build -d
📌 컨테이너가 정상적으로 실행되는지 확인
docker ps
docker logs -f moonrabbits-server-1
포트마다 컨테이너 여러 개 생성하기
services:
server:
build: . # 현재 디렉토리에서 Dockerfile을 빌드
ports:
- '3000-3002:3000' # 여러 개의 컨테이너를 실행할 경우 포트 범위를 지정
environment:
- PORT=
- HOST=
- CLIENT_VERSION=
- DB1_NAME=
- DB1_USER=
- DB1_PASSWORD=
- DB1_HOST=
- DB1_PORT=
- DB2_NAME=
- DB2_USER=
- DB2_PASSWORD=
- DB2_HOST=
- DB2_PORT=
- REDIS_HOST=
- REDIS_PORT=
- REDIS_PASSWORD=
deploy:
replicas: 3 # 동일한 컨테이너 3개 실행
restart: always # 컨테이너가 종료되면 자동으로 다시 시작
docker-compose up --scale server=3 -d
📌 scp로 Dockerfile과 docker-compose.yml 전송하기
scp -i new_key.pem Dockerfile docker-compose.yml ubuntu@1.2.3.4:/home/ubuntu/MoonRabbits
'JS > TIL(Today I Learned)' 카테고리의 다른 글
| 2025-03-03 <최종프로젝트 D-11> 레이턴시, 추측항법 구현과 사소한 버그 리포트 (1) | 2025.03.03 |
|---|---|
| 2025-02-25 <최종 프로젝트 D-14> MVP(Minimum Viable Product) 발표회 (0) | 2025.02.28 |
| 2025-02-26 (0) | 2025.02.26 |
| 2025-02-25 <최종 프로젝트 D-17> (0) | 2025.02.25 |
| 2025-02-24 <최종프로젝트 D-18> 채팅 기능 업그레이드 (0) | 2025.02.24 |
댓글