What Is the Difference between CMD and ENTRYPOINT in a Dockerfile?

Better Stack Team
Updated on August 1, 2022

There is a big confusion around similarity and lack of clarity in the difference between the CMD and ENTRYPOINT instruction in Docker. Let’s clear things up.

See the example

Let’s say we want to create a new Ubuntu image whose only purpose is to sleep for a while. The Dockerfile would look something like this:

 
FROM ubuntu
CMD sleep 10

And we would build and run the image like this:

 
docker build -t docker_sleep .
docker run docker_sleep
# sleep for 10 seconds

Just like this, we have executed the CMD instruction. But what actually happened under the hood is that the sleep 10 command was passed to the /bin/sh -c which is the default Linux shell, therefore the command was executed. But why is that, you may ask. This is because the default ENTRYPOINT is set to the /bin/sh -c and the sleep 10 is only the argument that is passed to the ENTRYPOINT.

If we were to build the image from the following Dockerfile:

 
FROM ubuntu
ENTRYPOINT sleep

And build run it like this:

 
docker run custom_sleep 20

It would do exactly the same as last time. Why is that, you may as again. This is because we have specified the ENTRYPOINT as sleep and the argument for the ENTRYPOINT as 20.

The difference

The ENTRYPOINT is the program that is going to be run and the CMD is the argument that is going to be passed to that program. Arguments passed to the container when running docker run <container> are in fact CMD and are passed to the ENTRYPOINT. Default ENTRYPOINT is set to the default Linux shell.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github