Mastering Docker's CMD and ENTRYPOINT

Eyecatch

Introduction

There are CMD and ENTRYPOINT as setting items of Dockerfile, but these differences are often confusing for those new to Docker. This is because both of these items can execute the command you want to run on the container when the container is executed. In this article, I will explain the difference between CMD and ENTRYPOINT through actual examples. We have prepared a simple model that you can try to understand by moving it into your environment.

CMD vs ENTRYPOINT

In conclusion, CMD and ENTRYPOINT have the following commonalities and differences. Let's take a look at a real-life example after understanding the characteristics of each in this section.

What CMD and ENTRYPOINT have in common

  • This command is executed during docker run.
  • You can override the command by passing an argument during docker run.
  • CMD can also be declared only once in each Dockerfile of ENTRYPOINT. If multiple declarations are made, the last CMD and ENTRYPOINT will be valid.

Features of CMD

  • CMD changes the default value of the command specified at docker run to the CMD command description in the Dockerfile.
  • If executed with the argument of docker run, the command part of CMD will be overwritten.

Features of ENTRYPOINT

  • ENTRYPOINT forces the command to be executed during docker run.
  • The argument at the time of docker run is executed as the argument of ENTRYPOINT. Unlike CMD, it is not overwritten.

CMD example

Running a Docker Container with CMD

Let's learn the details through the actual CMD execution example The following is a simple example of a Dockerfile that outputs a string on the command line.

FROM alpine
CMD ["echo", "Hello!"]

Save the Dockerfile and run the following command to build it.

$ docker build -t test .

If you execute the built container with the following command, you can see that the string Hello! Is output.

$ docker run --rm test
Hello!

On the other hand, if you execute it with the following options, you can see that the string AAAAAA is output and the string Hello! Is overwritten. In other words, it can be understood that the CMD command description is just the default value and can be overwritten.

$ docker run --rm test echo "AAAAAA"
AAAAAA

Docker's ENTRYPOINT Example (Exec Form)

Running a Docker Container with ENTRYPOINT

Let's learn the details in this case by actually executing ENTRYPOINT in exec form. The following is an example of a Dockerfile that uses the exec form of ENTRYPOINT, which outputs a character string on the command line.

FROM alpine
ENTRYPOINT ["echo", "Hello!"]

Save the Dockerfile in the same way, and execute the following command to build it.

$ docker build -t test .

If you execute the built container with the following command, you can see that the character string Hello! Is output as in the case of CMD.

$ docker run --rm test
Hello!

On the other hand, what if you run it with the following options? In this case, unlike ** CMD, the command is not overwritten and the argument content is executed in addition to the ENTRYPOINT content. ** This is a big difference.

$ docker run --rm test echo "AAAAAA"
Hello! echo AAAAA

Overwrite with --entrypoint option

On the other hand, in ENTRYPOINT, you can change the instruction by using the option of —entrypoint as follows.

docker run --entrypoint [COMMAND] [IMAGE] [OPTIONAL_VALUE]

The following is an example of executing the test container created in this paragraph with options. By giving the option in this way, the instruction can be overwritten and changed.

$ docker run --rm --entrypoint sh test -c'echo "test"'
test

Docker's ENTRYPOINT Example (Shell Form)

Then, what about the execution example in shell form even with the same ENTRY POINT? The following is an example of a Dockerfile that uses the exec form of ENTRYPOINT, which outputs a character string on the command line.

FROM alpine
ENTRYPOINT echo "Hello!"

Save the Dockerfile and run the following command to build it.

$ docker build -t test .

If you execute the built container with the following command, you can see that the character string Hello! Is output here as well as the exec form of CMD and ENTRYPOINT. So far, we can see that it behaves similarly in all cases.

$ docker run --rm test
Hello!

Now, let's execute it with the following options as in the exec form. In this case, unlike CMD and exec form ENTRYPOINT, you can see that the argument does not affect the behavior. In other words, even with the same ENTRYPOINT, the behavior differs between the exec form and the shell form, so be careful.

$ docker run --rm test echo "AAAAAA"
Hello!

Overwrite with --entrypoint option

However, even in the shell form, you can change the instruction by using the --entrypoint option as shown below.

The following is an example of executing this test container with options. As with the exec form, by adding options like this, the instruction can be overwritten and changed.

$ docker run --rm --entrypoint sh test -c'echo "test"'
test

Example of using CMD and ENTRYPOINT together

Finally, what is the behavior when using CMD and ENTRYPOINT together? In conclusion, the behavior is as follows.

  • If both ENTRYPOINT and CMD are present, what is written in CMD is executed as an option of the command written in ENTRYPOINT.
  • If an argument is added at the time of docker run, the contents of CMD will be overwritten and the ENTRYPOINT command will be executed.

Let's confirm this through the implementation example. The following is an example of using ENTRYPOINT and CMD together. At this time, note that both ENTRYPOINT and CMD are in exec form.

FROM alpine
ENTRYPOINT ["echo", "Hello,"]
CMD ["World"]

As before, execute the following command to build.

$ docker build -t test .

As a result, you can see that the character string defined by CMD is output after the character string defined by ENTRYPOINT. In other words, you can see that CMD is treated as an option for ENTRYPOINT.

$ docker run --rm test
Hello, World

But what if you specify the arguments as before? In this case, The contents of ENTRYPOINT are not changed as shown below, and the contents of CMD are overwritten as they are.

$ docker run --rm test "Alice!"
Hello, Alice

In this way, it is better to specify the command you want to execute with ENTRYPOINT and describe the highly changeable element following ENTRYPOINTS in CMD.

Conclusion

As mentioned above, CMD and ENTRYPOINT have similar roles and are confusing, but they have different functions. CMD, ENTRYPOINT, and ENTRYPOINT also behave differently between shell form and exec form, so it's a good idea to use each function properly. The instructions in the Dockerfile are a bit complicated, but you can use them effectively if you understand them.

Logo

Secure your IaC with just a few clicks!

You can keep your IaC security for free. No credit card required.

Share This Post