Difference between CMD vs ENTRYPOINT Docker Commands

Bikram
4 min readOct 9, 2021

--

Shell and Exec Form

Before we begin, it is important to discuss the forms of instructions. Docker ENTRYPOINT and CMD can have two forms:

  • Shell form
  • Exec form

The syntax for any command in shell form is:

<instruction> <command>

The syntax for instructions in exec form is:

<instruction> ["executable", "parameter"]

You can write Docker CMD/ENTRYPOINT instructions in both forms:

  • CMD echo "Hello World" (shell form)
  • CMD ["echo", "Hello World"] (exec form)
  • ENTRYPOINT echo "Hello World" (shell form)
  • ENTRYPOINT ["echo", "Hello World"] (exec form)

However, try to keep all your instructions in exec form to prevent potential performance issues.

ENTRYPOINT or CMD

  • Both ENTRYPOINT and CMD give you a way to identify which executable should be run when a container is started from your image..

Docker CMD

  1. Whenever we want to override executable while running the container, use CMD.
  2. We can override the value with a command-line argument.
  3. We can multiple CMD in a single docker file but only one will be executable while the container start.

We will see each point practically.

Let’s see an example.

vi Dockerfile

Add the following content to the docker file

FROM ubuntu
MAINTAINER Bikram
RUN apt-get update
CMD ["echo", "Hello World"]

Build the docker file

$ docker build -t ubuntu-test .
$ docker run -it ubuntu-test:latest
  • Here, we have passed as parameter Hello World for CMD that prints after container start.

Now let’s override the Hello World.

$ docker run -it ubuntu-test:latest echo "Hi!"

Here, we overrode the default value with Hi!

Now, we will modify the Dockerfile to add two CMDs.

FROM ubuntu
MAINTAINER Bikram
RUN apt-get update
CMD ["echo", "Hello Docker"]
CMD ["echo", "Hello Python"]

Create an image and then run the container

As we can see last CMD is the executable when the container starts.

Docker Entrypoint

  • ENTRYPOINT is the other instruction used to configure how the container will run.
  • Just like with CMD, you need to specify a command and parameters.

What is the difference between CMD and ENTRYPOINT?

You cannot override the ENTRYPOINT instruction by adding command-line parameters to the docker run command.

We will also see an example

2. Create a Dockerfile with ENTRYPOINT

FROM ubuntu
MAINTAINER sofija
RUN apt-get update
ENTRYPOINT ["echo", "Hello Google"]

Build docker image and run a container

$ docker build -t ubuntu-test .
$ docker run -it ubuntu-test:latest

It worked the same as CMD but now we will see the difference.

$ docker run -it ubuntu-test:latest "Hello AWS"

Above, we ran the docker run command with different parameters but the executable hasn’t overridden and also added a new parameter with the old parameter.

Docker Entrypoint with CMD

Now, let’s see how to use CMD and Entrypoint together.

1. First, we are going to modify our existing Dockerfile so it includes both instructions.

$ vi Dockerfile

2. The file should include an ENTRYPOINT instruction specifying the executable, as well as a CMD instruction defining the default parameter.

FROM ubuntu
MAINTAINER Bikram
RUN apt-get update
ENTRYPOINT ["echo", "Hello"]
CMD ["Google World"]

3. Build a new image from the modified Dockerfile and run a container.

$ docker build -t ubuntu-test .
$ docker run -it ubuntu-test:latest

It will return the message Hello Google World. However, what happens when we add parameters to the docker run command?

Use the same command again, but this time adds your name to the run command:

$ docker run -it ubuntu-test:latest Bikram

The output has now changed to Hello [your_name](in my case, it’s Hello Bikram). This is because you cannot override ENTRYPOINT instructions, whereas with CMD you can easily do so.

Conclusion

  • Whenever there is a chance of overriding executable while running the container using CMD otherwise uses ENTRYPOINT.
  • Sometimes we don’t have to override the executable all we want to run the container, in that case, ENTRYPOINT is the best use case.
  • If ENTRYPOINT is used for the executable, we can use CMD to pass default parameters. In that case, we use both together.

--

--

Bikram

Certified Kubernetes Administrator

Recommended from Medium

Lists

See more recommendations