궁금한게 많은 개발자 노트

[ Docker ] Docker Image가 컨테이너에서 실행 될 때 수행되는 명령어 본문

DevOps

[ Docker ] Docker Image가 컨테이너에서 실행 될 때 수행되는 명령어

궁금한게 많은 개발자 2024. 1. 23. 17:19

Dockerfile 작성 시 작성 된 Dockerfile에 의해 생성되는 Docker Image가 컨테이너에서 실행 될 때 수행되는 명령어를 정의하기 위해 ENTRYPOINT, CMD를 사용하곤 합니다.

이 때, ENTRYPOINT와 CMD의 차이점은 이전에 작성한 블로그에서 확인할 수 있듯 항상 수행되어야만 하는 명령어는 ENTRYPOINT에 작성하고 해당 명령어의 인자 또는 docker run명령에 의해 컨테이너가 실행될 때 인자로 주어지는 값에 의해 대체 될 수 있는 값은 CMD로 작성합니다.

 

 

하지만, Dockerfile작성 시에 ENTRYPOINT로 지정한 명령어도 대체될 수 있습니다.

k8s환경에서 해당 Docker Image를 통해 Pod내부에서 동작하는 Container들을 정의할 때, command와 args를 사용하여 컨테이너 실행 시 수행 될 명령어들을 정의할수 있습니다.

이를 통해 Dockerfile에 기술 된 ENTRYPOINT와 CMD는 command와 args에 의해 덮어써지게 됩니다.

 

https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#define-a-command-and-arguments-when-you-create-a-pod

 

Define a Command and Arguments for a Container

This page shows how to define commands and arguments when you run a container in a Pod. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run

kubernetes.io

The command and arguments that you define in the configuration file override the default command and arguments provided by the container image. If you define args, but do not define a command, the default command is used with your new arguments.
Note: The command field corresponds to entrypoint in some container runtimes.

 

https://people.wikimedia.org/~jayme/k8s-docs/v1.16/ko/docs/tasks/inject-data-application/define-command-argument-container/#%EC%B0%B8%EA%B3%A0%EC%82%AC%ED%95%AD

 

컨테이너를 위한 커맨드와 인자 정의하기

 

people.wikimedia.org

 

아래 표에서 확인할 수 있듯 컨테이너에서 실제 수행되는 명령어는 Dockerfile에서는 ENTRYPOINT로 정의하고, 쿠버네티스 Pod생성 시에는 command로 전달할 수 있습니다. 더 뒷 단계에서 정의되는 command에 의해 ENTRYPOINT는 대체되어 질수 있고, 이와 마찬가지로 CMD는 args에 의해 대체될 수 있습니다.

 

아래에 다양한 예시를 통해 이해를 도울 수 있을 것 같습니다.

Comments