Docker CMD & ENTRYPOINT Differences Explained in Detail

dockerfile, dockerfile cmd, dockerfile entrypoint, cmd vs entrypoint, dockerfile cmd and entrypoint differences, dockerfile cmd vs entrypoint examples

This post explains you the detailed differences between Docker CMD and Docker ENTRYPOINT instructions available in dockerfile. To know more about other dockerfile instructions, refer this link Dockerfile Instructions Explained with Examples.

Docker CMD and Docker ENTRYPOINT command instructions are used to set a command to be executed when running a container as an executable. Both instructions are used for same purpose but not similar in functionality. So we must be aware of which instruction to be used for our requirement.

Dockerfile CMD & ENTRYPOINT Differences Explained in Detail

Both declaration can be used in Shell form or Executable form. Also both has the same syntax as below.

Example of CMD instruction:
Shell form:
CMD ping google.com
CMD python myapplication.py
Executable form:
CMD ["ping","google.com"]
CMD ["python","myapplication.py"]

Example of ENTRYPOINT:
Shell form:
ENTRYPOINT ping google.com
ENTRYPOINT python myapplication.py
Executable form:
ENTRYPOINT ["ping","google.com"]
ENTRYPOINT ["python","myapplication.py"]
Let's see one by one with an example to explain this better.

For this, We create two dockerfiles, one with CMD instruction and another one with ENTRYPOINT instruction to run a same simple script which executes "ping google.com" as an executable.

Dockerfile 1 : With CMD instruction.
Here is a dockerfile which has CMD instruction runs our simple script in a container as an executable.
FROM docker.io/centos:latest
MAINTAINER Devops Engineer
WORKDIR /data
RUN echo 'ping google.com' > runapp.sh
CMD sh runapp.sh
Image has already built using "docker build ." command and created a image tag "learnitguide.net/cmdtest".

Dockerfile 2 : With ENTRYPOINT instruction.
Here is a dockerfile which has ENTRYPOINT instruction.
FROM docker.io/centos:latest
MAINTAINER Devops Engineer
WORKDIR /data
RUN echo 'ping google.com' > runapp.sh
ENTRYPOINT sh runapp.sh
Image has already built using "docker build ." and created a tag "learnitguide.net/entrypointtest".

Testing the Containers
[ads-post]
Lets run a containers with newly created images (learnitguide.net/cmdtest and learnitguide.net/entrypointtest).
[root@docker-host ~]# docker run -d -it --name cmdtest1 learnitguide.net/cmdtest
0eb048ff4218472871c99492c57e5369badfeeb7771ea73ba8d3d44f653ca839
[root@docker-host ~]# docker run -d -it --name entrypointtest1 learnitguide.net/entrypointtest
ea5a97405ee67e2fc5b3cfe9914ac3059ea542844a2ff312d7ebac6f7b613c70
[root@docker-host ~]#
Check the logs of newly created containers (cmdtest1 and entrypointtest1) to ensure the application / script has run or not.
[root@docker-host ~]# docker logs cmdtest1
PING google.com (172.217.24.206) 56(84) bytes of data.
64 bytes from 2a00:1450:400f:809::200e: icmp_seq=1 ttl=54 time=29.5 ms
64 bytes from 2a00:1450:400f:809::200e: icmp_seq=2 ttl=54 time=29.2 ms

[root@docker-host ~]# docker logs entrypointtest1
PING google.com (172.217.24.206) 56(84) bytes of data.
64 bytes from 2a00:1450:400f:809::200e: icmp_seq=1 ttl=54 time=28.9 ms
64 bytes from 2a00:1450:400f:809::200e: icmp_seq=2 ttl=54 time=29.0 ms

[root@docker-host ~]#
Both containers runs the application / script without any issues.

What happens if we use any arguements at the end of "docker run" command. For example, lets run a new containers with "date" command.
[root@docker-host ~]# docker run -d -it --name cmdtest2 learnitguide.net/cmdtest date
38ab7707591c7a2e0640180aedf66d3bebbd881f746d073aaad72fb78f509145
[root@docker-host ~]# docker run -d -it --name entrypointtest2 learnitguide.net/entrypointtest date
4e244d8a6f682d6a0b086454fec71575f9bc64a5f9a75f3598eb068fcc2a1a3e
Again check the logs of newly created containers (cmdtest2 and entrypointtest2) to ensure the application / script has run or not.
[root@docker-host ~]# docker logs cmdtest2
Wed Jun 27 17:23:51 UTC 2018
[root@docker-host ~]# docker logs entrypointtest2
PING google.com (216.58.220.206) 56(84) bytes of data.
64 bytes from 2a00:1450:400f:809::200e: icmp_seq=1 ttl=54 time=29.5 ms
64 bytes from 2a00:1450:400f:809::200e: icmp_seq=2 ttl=54 time=29.3 ms
Above logs shows differently than before when we used extra arguments at the end of the "Docker run" command. This is where the actual problem occurs, our script is not running in our "cmdtest2" container but it works on "entrypointtest2" container.

Why?

Because, When a user run a container with any arguments (commands) at the end of "docker run" command, the specified commands override the default arguement in CMD instruction, so the container will run the arguement given at the end of the docker run command.

But if the same argument is given along with ENTRYPOINT instruction in dockerfile, even when a user gives any argument at the end of the docker run command, that will not override ENTRYPOINT instruction. So instruction will run as it is.

Hope you have got an idea on both CMD and ENTRYPOINT instructions. Going forward, we will play more with docker tool. Keep practicing and have fun.

Also refer other articles,

What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
Dockerfile Instructions Explained with Examples
How to Write Dockerfile To Build Your Own Docker Images
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.
YouTube | Facebook | Twitter | Pinterest | Rss
Keywords : dockerfile, docker file, docker file cmd, dockerfile entrypoint, cmd vs entrypoint, dockerfile cmd and entrypoint differences, dockerfile cmd vs entrypoint examples, difference cmd vs entrypoint, writing dockerfiles, dockerfile examples, dockerfile cmd examples, dockerfile entrypoint examples, dockerfile cmd arguments, dockerfile entrypoint arguments, dockerfile cmd explained, dockerfile entrypoint explained
June 27, 2018

Post a Comment

[disqus][facebook][blogger]

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget