DOCKER CMD vs ENTRYPOINT

Aakash Shinghal
6 min readJan 29, 2022

--

CONTAINERS

1. Introduction

Let’s start with a simple example. Let’s say you want to run a docker container from an ubuntu image. When you run, docker run ubuntu command, it runs an instance of Ubuntu image and exists immediately.

If you will list the running containers, you will not see any running ubuntu containers.

But if you will list all the containers present, then you will see the ubuntu container in an exited state.

Now, why is that? Containers are designed for running specific tasks and processes, not for hosting operating systems. A container is created to serve a single unit task. Once it completes the given task, it stops. Therefore, the container’s life cycle depends on the ongoing process inside of it. Once the process stops, the container stops as well.

2. DockerFile

A Dockerfile defines what processes to run within a container. Dockerfile is a text document that contains a set of instructions on how to build a docker image and what processes to run within a container spin out of the image.

In a Dockerfile, there are two types of instructions that can define the processes running inside the container:

  1. CMD
  2. ENTRYPOINT

If we look at the DockerFile of ubuntu image, we will see an instruction called CMD, which stands for COMMAND, and that defines the program which will run inside the container when the container will be running.

Now for this ubuntu image, we are seeing bash as a CMD command. Now bash is not really a process like any web server or database server. It is a shell that listens for input from a terminal, and if it doesn’t find the terminal, then it exits. When we ran the ubuntu container from the ubuntu image, docker didn’t attach any terminal by default to the ubuntu container. So the bash program didn’t find the terminal and it exited.

3. SHELL AND EXEC Form

Docker CMD and ENTRYPOINT can have two forms of instructions:

  • Shell form
  • Exec Form

The syntax for any command in shell form is <instruction> <command>

The syntax for any command in exec form is <instruction> [“executable”, “paramter”]

Example:

  • 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.

4. CMD

CMD defines default commands for an executing container.

CMD is best to use when you need a default command that a user can override. If the user specifies arguments to docker run then they will override the default specified in CMD.

Let’s again take the example of the ubuntu image to understand.

Firstly, let’s run the ubuntu image by using the command docker run ubuntu and notice the default command (bash)got executed.

This bash command is the default command as mentioned in the Ubuntu DockerFile.

Now, let’s try to override this bash command. Let’s run the ubuntu container using the sleep command. docker run ubuntu sleep 5

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

5. ENTRYPOINT

Entrypoint is preferred when you want to define a container with a specific executable.

All command-line arguments will be appended to ENTRYPOINT parameters and will not be overridden by adding command-line parameters to docker run.

Let’s take the example of a new image ubuntusleeper to understand this in detail.

Firstly, let’s run ubuntusleeper image by using command docker run ubuntusleeper and notice the default command (sleep 5)got executed.

This sleep 5 command is the ENTRYPOINT command as mentioned in the ubuntusleeper DockerFile.

Now, let’s try to override this sleep 5 command. Let’s run ubuntusleeper container using sleep command. docker run ubuntusleeper sleep 10

You can notice in the above images that this time it didn’t override the existing sleep 5 command, but sleep 10 got appended after it.

Note: Entrypoint cannot be overridden unless you add the --entrypoint flag.

6. Docker EntryPoint vs CMD

As you have seen so far, ENTRYPOINT and CMD are similar, but not the same. What’s more, these two instructions are not mutually exclusive, which means we can have both CMD and ENTRYPOINT in our command. In such cases, the executable is defined with ENTRYPOINT, while CMD specifies the default parameter.

If you are using both instructions, make sure to keep them in exec form.

Now, let’s take again take the example of ubuntusleeper image, and modify it’s Dockerfile to use both CMD and ENTRYPOINT.

In this above Dockerfile, we have defined sleep command as executable and provided the default value in CMD.

Let’s run ubuntusleeper image and notice the command executed.

Now, if we pass any value during the run time, then it will append after ENTRYPOINT executable and will override the existing CMD value.

7. Conclusion

Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are a few rules that describe their co-operation.

  1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands.
  2. ENTRYPOINT should be defined when using the container as an executable.
  3. CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
  4. CMD will be overridden when running the container with alternative arguments.

I hope you enjoyed reading this article, as much as I enjoyed writing it. If you like this article please let me know! But, more importantly if you disagree with this article please, please, please let me know! I made this with the hope of helping the community so if it is off it defeats the purpose! If you have a suggestion or critique please feel free to drop in any comments.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Aakash Shinghal
Aakash Shinghal

No responses yet

Write a response