Dockerfile 명령어 중 ENTRYPOINT 명령과 CMD 명령은 모두 이미지가 실행될 때(이미지가 컨테이너로 활성화될 때) 실행할 내용을 지정한다. ENTRYPOINT 명령과 CMD 명령은 헷갈리기 쉽다. 그 차이를 정리해본다.
※ RUN 명령어는 이미지가 생성될 때 실행되는 명령이다. ENTRYPOINT 혹은 CMD와는 다르다.
- Dockerfile의 CMD는 docker-compose의 command 지시자의 내용으로 교체된다.
ENTRYPOINT 명령은 그 내용이 교체되지 않는다.
예)
▶ Dockerfile
FROM ubuntu:latest
MAINTAINER admin@abc.com
WORKDIR /root
CMD ["/bin/bash", "-c", "echo 'CMD'"]
▶ docker-compose.yml
version: "3.3"
services:
exercise:
image: ubuntu_20.04:exercise
container_name: "docker-exercise"
command: ["/bin/bash", "-c", "echo 'command'"]
이미지를 생성하고 docker-compose를 실행해서 결과를 보면 아래와 같다.
$> docker build -t ubuntu_20.04:exercise .
$> docker-compose up
Creating network "docker-exercise_default" with the default driver
Creating docker-exercise ... done
Attaching to docker-exercise
docker-exercise | command
docker-exercise exited with code 0
Dockerfile의 CMD 명령은 이미지 실행 시점에 docker-compose.yml 파일의 command 명령에 의해 대체된다.
- ENTRYPOINT와 CMD는 함께 사용될 수 없다. 함께 사용될 경우 ENTRYPOINT 내용 만 실행된다.
예)
▶ Dockerfile
FROM ubuntu:latest
MAINTAINER admin@abc.com
WORKDIR /root
CMD ["/bin/bash", "-c", "echo 'CMD'"]
ENTRYPOINT ["/bin/bash", "-c", "echo 'ENTRYPOINT'"]
▶ docker-compose.yml
version: "3.3"
services:
exercise:
image: ubuntu_20.04:exercise
container_name: "docker-exercise"
command: ["/bin/bash", "-c", "echo 'command'"]
이미지를 빌드하고 docker-compose를 실행해서 결과를 살펴보면 아래와 같다.
$> docker build -t ubuntu_20.04:exercise .
$> docker-compose up
Creating network "docker-exercise_default" with the default driver
Creating docker-exercise ... done
Attaching to docker-exercise
docker-exercise | ENTRYPOINT
docker-exercise exited with code 0
즉, ENTRYPOINT 명령에 기술된 echo 'ENTRYPOINT'만 실행된다.
Dockerfile 명령 중 ENTRYPOINT와 CMD의 차이를 살펴보았다. 이미지가 실행될 때 실행될 명령을 지정하는 목적으로 ENTRYPOINT, CMD 어떤 명령을 사용해도 무방하다. 개인적으로 필자는 사용자의 간섭이 없는 docker 실행 환경을 지향한다는 취지에서 ENTRYPOINT를 더 자주 사용한다.
■
'풀스택 개발 들여다보기' 카테고리의 다른 글
[Docker] 웹 메일 서버 dockerizing (0) | 2022.04.04 |
---|---|
[Docker] Dockerizing 예 - 파이썬 플라스크 서버 (0) | 2022.03.23 |
[Docker] Dockerfile 명령어 살펴보기 (0) | 2022.03.16 |
[Docker] 도커 이미지 빌드 - Dockerfile 기본 구조 (0) | 2022.03.15 |
[Docker] 컨테이너를 이미지로 저장하기 - 도커 이미지 생성 (0) | 2022.03.09 |
댓글