Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on swebtools.com

Search Suggest

Docker CMD & ENTRYPOINT Differences Explained in Detail

docker cmd commands, docker entrypoint commands, docker cmd examples, docker entrypoint examples, docker cmd vs entrypoint differences
This tutorial post will help you to understand the Docker CMD & ENTRYPOINT differences and explained the instructions available in dockerfile. To know more about other dockerfile instructions, refer this link Dockerfile Instructions Explained with Examples.

Docker CMD & ENTRYPOINT Differences Explained in Detail


Docker CMD & ENTRYPOINT differences are not much, these two 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

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 about Docker CMD & ENTRYPOINT differences. Going forward, we will play more with docker tool.

If you are interested in learning, Request you to go through the below recommended tutorial.

DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.