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 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.
-
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 -
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
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github