Difference Between CMD and ENTRYPOINT in Dockerfile

marounmaroun profile image Maroun Maroun ・1 min read

I'll try to explain the differences through a simple example.

Let's suppose we want to create an Ubuntu-image that will always run a sleep command when it starts. We'll create our own image and specify a new command:

FROM ubuntu
CMD sleep 10

Now, we build the image:

docker build -t custom_sleep .
docker run custom_sleep
# sleeps for 10 seconds and exits

What if we want to have more sleep? Since the value is hardcoded in our Dockerfile, we'll need to edit it or override the whole command:

# "sleep 20" is the command we now pass
docker run custom_sleep sleep 20

Hmmm... Since the container's main purpose is to sleep, it sounds a bit ridiculous to pass the "sleep" command to it.

Now let's try using the ENTRYPOINT instruction:

FROM ubuntu

This instruction specifies the program that will be run when the container starts.

Now we can run:

docker run custom_sleep 20

Ah! Much better. But what about a default value? Well, you guessed it right:

FROM ubuntu
ENTRYPOINT ["sleep"]
CMD ["10"]

The ENTRYPOINT is the program that will be run, and the value passed to the container will be appended to it. It can be overridden by specifying an --entrypoint flag, followed by the new entry point you want to use.

Posted on by:

marounmaroun profile

Maroun Maroun


Nice to meet you 🍻 My name is Maroun. I'm a dad, musician, programmer, Stackoverflower and astronaut wannabe.


Editor guide