DEV Community

Maroun Maroun
Maroun Maroun

Posted on

3

Difference Between CMD and ENTRYPOINT in Dockerfile

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
ENTRYPOINT sleep

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.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more