The difference between RUN CMD and ENTRYPOINT in Dockerfile

The RUN, CMD, and ENTRTPOINT instructions in the Dockerfile can all be used to specify the instructions running in the container, but there are subtle differences between the three.

simply put:

RUN

The RUN command is generally used to install software packages in the container or execute other commands, such as

RUN yum install -y telnet
RUN touch web.xml

CMD

The CMD command is mainly used to indicate the command and parameters of the generated Docker image at startup. This command can be replaced by the command after docker run, such as the following Dockerfile file

FROM busybox
CMD echo "hello world"

CMD specifies that the Docker image outputs a "hello world" at runtime

[root@bochs Docker]# docker build -t test .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM busybox
 ---> 83aa35aa1c79
Step 2/2 : CMD echo "hello world"
 ---> Running in a1a4d74137d2
Removing intermediate container a1a4d74137d2
 ---> 651b45b58fe9
Successfully built 651b45b58fe9
Successfully tagged test:latest
[root@bochs Docker]# docker run -it test
hello world

But if you add other instructions after docker run. then CMD will be replaced directly

[root@bochs Docker]# docker run -it test ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var

ENTRYPOINT

ENTRYPOINT is similar to CMD, the difference is that ENTRYPOINT must be executed. If both ENTRYPOINT and

CMD, the parameters in CMD will be passed to ENTRYPOINT as additional parameters.

[root@bochs Docker]# cat Dockerfile 
FROM busybox
ENTRYPOINT ["/bin/echo","hello"]
CMD ["world"]

To run through docker run, CMD becomes the parameter of ENTRYPOINT:

[root@bochs Docker]# docker run -it test2 
hello world

But if you specify the parameter china of docker run, the output will become:

[root@bochs Docker]# docker run -it test2 china
hello china

The parameter world in the original CMD is replaced by china in docker run, but the hello that comes with ENTRYPOINT is still output normally

Shell and Exec formats

CMD, RUN, ENTRYPOINT can use two formats to pass commands and parameters. Shell is generally expressed as command + command, such as:

RUN yum install -y telnet
CMD echo "hello world"

The first capitalized word is the instruction of the Dockerfile. It is followed by the command, which can be executed separately in the shell

The Exec format can be expressed as: instruction+["command","command parameter 1","command parameter 2",...], for example:

RUN ["yum","install","telnet"]
ENTRYPOINT ["/bin/bash","-c","echo hello world"]

For these two formats, it is best to use the Exec format for CMD and ENTRYPOINT. The commands and parameters are separated, and the hierarchy is strong, while RUN can be used.

Note: The Shell format and Exec format of ENTRYPOINT are very different

For example, the following Shell format ENTRYPOINT

FROM busybox
ENTRYPOINT echo "hello"
CMD "world"

When running the generated container, only hello will be output, and the "world" in CMD will be ignored. The same docker run parameters will also be ignored

[root@bochs Docker]# docker run -it test
hello
[root@bochs Docker]# docker run -it test china
hello

Tags: Docker

Posted by tullmejs on Sun, 22 May 2022 11:09:05 +0300