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
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"]
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.