What Is the Difference between CMD and ENTRYPOINT in a Dockerfile?
There is a big confusion around similarity and lack of clarity in the difference between the
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
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
sleep and the argument for 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 is set to the default Linux shell.
How Do I Pass Environment Variables to Docker Containers?
It is always a good practice to separate the app from its configuration. It is not a good idea to have a database login credential defined as variables in the code of the application. This is why w...Questions
How to Connect to the Localhost of the Machine from inside of a Docker Container?
If you are running some kind of a server or any other service (such as a database) on localhost and the service isn’t exposing any port, you may have found yourself wondering how to connect to this...Questions
How to Copy Docker Images from One Host to Another without Using a Repository
There is an easy way to transfer Docker images from one host to another without using any repository. Copy Docker images from one host to another First, you need to save the docker image as a tar: ...Questions
Solved: Cron missing newline before EOF
This error may also happen when a crontab file is generated automatically and the generator failed to insert a newline character at the end of the crontab file.Questions
We are hiring.
Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.
Help us in making the internet more reliable.
Help us with developer education and get paid.
Reliability is the
Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.Explore Better Stack