Overview

Find out what role ENTRYPOINT and CMD play in Dockerfile and see the difference between the two commands.

Untitled

ENTRYPOINT

ENTRYPOINT defines the command to execute when the container is started. The first factor refers to the command to execute and the remaining factors refer to the parameter.

1FROM centos:7
2ENTRYPOINT ["echo", "Hello, Penguin"]

If you do it as above, you will receive “Hello, Penguin” as a factor in the echo command as below and output it.

1$ $ docker run hello-world
2Hello, Penguin

CMD

CMD provides the default parameters passed to ENTRYPOINT or defines the default commands to execute when ENTRYPOINT is not provided.

When you write a simple dockerfile, you’ll choose either CMD or ENTRYPOINT. So you might think that the two commands are used in a similar sense, but to be precise, CMD is an auxiliary role of ENTRYPOINT. If you look at the example code below, you’ll understand it a little more.

The Hello, Penguin!!! code I wrote above can be written like this by combining ENTRYPOINT and CMD.

CMD used as a parameter for ENTRYPOINT

1FROM centos:7
2CMD ["Hello Penguin!!!"]
3ENTRYPOINT ["echo"]
1$ $ docker run hello-world
2Hello, Penguin!!!

CMD is used alone and used as a command

1FROM centos:7
2CMD ["echo", "Hello Penguin!!!"]
1$ $ docker run hello-world
2Hello, Penguin!!!

As described above, CMD serves as a parameter for ENTRYPOINT or as a definition of the basic command to execute when used alone.

Use Examples

Using these characteristics of ENTRYPOINT and CMD, you can.

  • ‘ENTRYPONT’ is performed while executing echo commands
  • CMD sets the default value for echo factor: Hello, World!
  • When the docker is run, the value of CMD is overriding to the value of the user factor when the docker is delivered as a factor value.

I’m going to edit and paste the code that I just wrote with the example of CMD.

1FROM centos:7
2CMD ["Hello, World"]
3ENTRYPOINT ["echo"]

If you hand over the value of “Hello, Penguin” when docker is performed like this, the value of “CMD” is ignored and replaced by the value entered by the user.

1$ $ docker run hello-world
2Hello, World
3
4$ $ docker run hello-world 'Hello, Penguin'
5Hello, Penguin

To summarize use examples, CMD can flexibly handle requests and allow users to change commands at execution time. ENTRYPOINT forces the container to execute specific commands

CMD/ENTRYPOINT Correlation

The correlation between CMD and ENTRYPOINT is summarized in a table as follows.

No ENTRYPOINTENTRYPOINT exec_entry p1_entryENTRYPOINT [“exec_entry”, “p1_entry”]
No CMDerror, not allowed/bin/sh -c exec_entry p1_entry
CMD [“exec_cmd”, “p1_cmd”]exec_cmd p1_cmd/bin/sh -c exec_entry p1_entry
CMD exec_cmd p1_cmd/bin/sh -c exec_cmd p1_cmd/bin/sh -c exec_entry p1_entry

Clean up

Through this article, we learned about the concepts and differences between CMD and ENTRYPOINT. When used in practice, there are cases where you use only ENTRYPOINT or CMD without much thought, so I think it would be a good idea to make a flexible container by combining them properly