First, the following commands, such as Run, CMD, and entrypoint, can be used to execute commands, but each has different characteristics.
- Run is executed at the time of build image.
- The CMD entrypoint is executed when the image is run.
- CMD can be used in conjunction with entrypoint, or it can be used with a single command. When both CMD and entrypoint are present in a dockerfile, the information set in cmd (the exec format) is provided to the entrypoint command in the form of parameters. entrypoint command is not set, the default is/bin/bash
- EntryPoint commands cannot be overridden simply, but can be overridden using the--entrypoint in Run, which is appended to the content set in EntryPoint if the Docker run provides parameters.
- When CMD is used alone, if parameters are provided later in the Run command, then the parameters will completely overwrite the cmd
Take a look at an example to illustrate the above, our approximate step is to create a simple Docker Image, using the echo command to see the difference between the cmd command and the entrypoint command in the case of separate use, combined use, and so on.
- The first time our dockerfile is as follows, I only use the cmd command
From CentOS
maintainer [email protected]
CMD ["/bin/dir", "/bin"]
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker build-t echotest:cmd-f dockerfile_cmd.
Sending build context to Docker daemon 4.096 KB
Step 1:from CentOS
---> 87E5B6B3CCC1
Step 2:maintainer [email protected]
---> Using cache
---> a9bea6e7d663
Step 3:cmd/bin/dir/bin
---> Running in abc9360528ee
---> 07103bc09e6f
Removing intermediate container Abc9360528ee
Successfully built 07103bc09e6f
Then we execute run and do not provide any parameters
650) this.width=650; "title=" clip_image001 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image001 "src=" http://s3.51cto.com/wyfs02/M02/87/40/wKiom1fY55_ Tlrokaaamggpludo987.png "" 759 "height="/>
Then we supply the parameters in case:
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it Echotest:cmd/bin/echo
# The above command does not have any output, normally it should be the following output (because the/bin/dir command is provided in our cmd command:
[Email protected]:~/dockerfiles/cmd_entrypoint$ Dir/bin/echo
/bin/echo
# I proceeded to execute the following command, and found that the parameters we provided completely replaced all the commands in cmd (/bin/dir/bin was completely replaced by/bin/echo test, validating the 5th of the previous summary)
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest:cmd/bin/echo test
Test
- The second test, our dockerfile content, is as follows, and we test using only the EntryPoint case.
[Email protected]:~/dockerfiles/cmd_entrypoint$ Cat Dockerfile_entrypoint
From CentOS
maintainer [email protected]
entrypoint ["/bin/echo", "Defaultparam"]
#CMD ["/bin/dir", "/bin"]
# and then we build another image Echotest:entrypoint
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker build-t echotest:entrypoint-f dockerfile_entrypoint.
Sending build context to Docker daemon 4.096 KB
Step 1:from CentOS
---> 87E5B6B3CCC1
Step 2:maintainer [email protected]
---> Using cache
---> a9bea6e7d663
Step 3:entrypoint/bin/echo Defaultparam
---> Running in F17009DD0DC1
---> 9f7dab52dc3a
Removing intermediate container F17009DD0DC1
Successfully built 9F7DAB52DC3A
# Executed with default parameters
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest:entrypoint
Defaultparam
# Given the parameters, we find that param1 is not like cmd, CMD is to take the parameters provided by Docker run to completely overwrite the cmd content, and entrypoint is to append the parameters to the entrypoint content.
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest:entrypoint param1
Defaultparam param1
# If you want to replace the contents of Entrypont, you can use Docker run--entrypointdocker
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it--entrypoint/bin/ping echotest:entrypoint param1
Ping:unknown host param1
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it--entrypoint/bin/ping echotest:entrypoint localhost
PING localhost (127.0.0.1) bytes of data.
bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.051 ms
bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.128 ms
bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.058 ms
bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.068 ms
^c
---localhost ping statistics---
4 packets transmitted, 4 received, 0% packet loss, time 2997ms
RTT Min/avg/max/mdev = 0.051/0.076/0.128/0.031 ms
[Email protected] vagrant]# Docker run--rm--entrypoint "/bin/bash" echotest:entrypoint-c "ping www.bing. COM"
PING cn.a-0001.a-msedge.net (202.89.233.103) bytes of data.
Bytes from 202.89.233.103:icmp_seq=1 ttl=115 time=37.1 ms
Bytes from 202.89.233.103:icmp_seq=2 ttl=115 time=37.2 ms
Bytes from 202.89.233.103:icmp_seq=3 ttl=115 time=36.9 ms
Bytes from 202.89.233.103:icmp_seq=4 ttl=115 time=36.7 ms
- The third Test, our dockerfile content, is as follows, and we test the use of both entrypoint and CMD.
First look at the contents of the Dockerfile, I deliberately set the cmd and the command executed in entrypoint to different
[Email protected]:~/dockerfiles/cmd_entrypoint$ Cat Dockerfile_cmd_and_entrypoint
From CentOS
maintainer [email protected]
entrypoint ["/bin/echo", "Defaultparam"]
CMD ["/bin/dir", "/bin"]
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker build-t echotest:entrypointandcmd-f dockerfile_cmd_and_ EntryPoint.
Sending build context to Docker daemon 4.096 KB
Step 1:from CentOS
---> 87E5B6B3CCC1
Step 2:maintainer [email protected]
---> Using cache
---> a9bea6e7d663
Step 3:entrypoint/bin/echo Defaultparam
---> Using cache
---> 9f7dab52dc3a
Step 4:cmd/bin/dir/bin
---> Running in 0bb1e042a2a0
---> 91eb9487a88a
Removing intermediate container 0bb1e042a2a0
Successfully built 91eb9487a88a
# I find here that when the entrypoint and CMD commands are present, the contents of CMD are supplied as parameters to the entrypoint command when executed with the default parameters. Let's see if we can change the order of ENTRYPOINT commands and CMD commands in dockerfile to see if there are any effects.
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest:entrypointandcmd
Defaultparam/bin/dir/bin
# When the parameters are executed, the contents of the cmd are replaced by the parameters provided by our Docker run, and then supplied to the entrypoint command with parameters.
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest:entrypointandcmd paramcus
Defaultparam Paramcus
# Overwrite entrypoint content with Docker run--entrypoint.
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it--entrypoint/bin/ls echotest:entrypointandcmd Paramcus
/bin/ls:cannot access paramcus:no such file or directory
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it--entrypoint/bin/ls echotest:entrypointandcmd /sbin
Addgnupghome build-locale-archive cracklib-packer Fsck.cramfs GRPCK
# Here's a test of the dockerfile if the order of CMD and entrypoint is reversed and if it has an effect on the result, the result is that there is no effect.
[Email protected]:~/dockerfiles/cmd_entrypoint$ Cat Dockerfile
From CentOS
maintainer [email protected]
CMD ["/bin/dir", "/bin"]
entrypoint ["/bin/echo", "Defaultparam"]
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker build-t echotest.
Sending build context to Docker daemon 5.12 KB
Step 1:from CentOS
---> 87E5B6B3CCC1
Step 2:maintainer [email protected]
---> Using cache
---> a9bea6e7d663
Step 3:cmd/bin/dir/bin
---> Using cache
---> 07103bc09e6f
Step 4:entrypoint/bin/echo Defaultparam
---> Running in 091b72e36f4f
---> B01150245beb
Removing intermediate container 091B72E36F4F
Successfully built B01150245beb
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest
Defaultparam/bin/dir/bin
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it echotest paramcus
Defaultparam Paramcus
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it--entrypoint/bin/ls echotest paramcus
/bin/ls:cannot access paramcus:no such file or directory
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker run--rm-it--entrypoint/bin/ls echotest/sbin
Addgnupghome build-locale-archive cracklib-packer Fsck.cramfs GRPCK
Cleanup site:
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker Images echotest*
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Echotest latest B01150245beb 4 minutes ago 224 MB
Echotest entrypointandcmd 91eb9487a88a minutes ago 224 MB
Echotest entrypoint 9f7dab52dc3a-minutes ago 224 MB
echotest cmd 07103bc09e6f minutes ago 224 MB
[Email protected]:~/dockerfiles/cmd_entrypoint$ Docker RMI $ (Docker images-q echotest*)
Untagged:echotest:latest
deleted:b01150245bebc9ff2c44ed259861a5466fbaf1f9bc8339a4ed965e38299e3380
Untagged:echotest:entrypointAndCmd
Deleted:91eb9487a88a70af6e91568018b11ca52574d47ffaed8b2600f24897027252d8
Untagged:echotest:entrypoint
deleted:9f7dab52dc3a33693743ffc34b463f820b3e954291927d73ca09e29c43fde80a
Untagged:echotest:cmd
deleted:07103bc09e6f918199f4a9f34dd13f4bf1eaab8bf415d5b70e360dc6885b5f9b
Description of the cmd command and the entrypoint command in Dockerfile