[Docker] RUN/CMD/ENTRYPOINT

Rex Chiang
3 min readMar 11, 2022

Forms of Dockerfile

  • Shell Form
RUN pip3 install -r requirements.txt
CMD echo Hello world
ENTRYPOINT echo Hello world

# In image generation, will transfer to exec form as below

RUN /bin/sh -c pip3 install -r requirements.txt
CMD ["/bin/sh" "-c" "echo Hello world"]
ENTRYPOINT ["/bin/sh" "-c" "echo Hello world"]
  • Exec Form
RUN ["pip3", "install", "-r", "requirements.txt"]
CMD ["echo", "Hello world"]
ENTRYPOINT ["echo", "Hello world"]

# In image generation, will transfer to exec form as below

RUN pip3 install -r requirements.txt
CMD ["echo" "Hello world"]
ENTRYPOINT ["echo" "Hello world"]

RUN

  • Execute commands in a new layer and commit to create a new image.
  • Execute commands during image generation, instead of container execution.
  • Will use the the result in cache when you try to rebuild the image, but you can avoid this by using — no-cache in argument.
  • Usually used for installing software packages or fixed updating.

CMD

< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
CMD ["echo", "Hello Word"]
< Terminal >% docker image build -t hello .
% docker run -it hello
% Hello Word
  • Set a default command when container runs.
  • If there are many CMD in Dockerfile, only the last command is valid.
  • Usually used for unfixed command.
< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
CMD ["echo", "Hello Word"]
< Terminal >% docker image build -t hello .
% docker run -it hello echo Hello Rex
% Hello Rex
  • If container runs with a command, the default CMD will be replaced.
< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENTRYPOINT ["echo", "Hello"]
CMD ["Rex"]
< Terminal >% docker image build -t hello .
% docker run -it hello
% Hello Rex
  • If there are no executable element, then it will be the parameter of ENTRYPOINT.

ENTRYPOINT

< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENTRYPOINT ["echo", "Hello Word"]
< Terminal >% docker image build -t hello .
% docker run -it hello
% Hello Word
  • Configure the container to executable, and raise error in container execution if it wasn’t set.
  • Set command when container runs.
  • If there are many commands in Dockerfile, only the last command is valid.
  • Usually used for fixed command.
< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENTRYPOINT ["echo", "Hello"]
< Terminal >% docker image build -t hello .
% docker run -it hello Rex
% Hello Rex
  • If container runs with argument, then it will be the parameter of ENTRYPOINT.

Advanced Usage

< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENTRYPOINT ["echo", "Hello"]
CMD ["Word"]
< Terminal >% docker image build -t hello .
% docker run -it hello Rex
% Hello Rex
  • If container runs with argument, then the default CMD will be replaced, and it will be the parameter of ENTRYPOINT.
< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENTRYPOINT echo Hello
CMD ["Word"]
< Terminal >% docker image build -t hello .
% docker run -it hello Rex
% Hello
  • If use shell form in ENTRYPOINT, then the CMD will not be the parameter of ENTRYPOINT.
< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENV name Rex
ENTRYPOINT echo Hello $name
< Terminal >% docker image build -t hello .
% docker run -it hello Rex
% Hello Rex
  • If use shell form, it can work with variable substituted.
< Dockerfile >FROM ubuntu
LABEL maintainer = "Rex"
ENV name Rex
ENTRYPOINT ["echo", "Hello $name"]
< Terminal >% docker image build -t hello .
% docker run -it hello Rex
% Hello $name
  • If use exec form, it can’t work with variable substituted.

--

--