Docker CMD vs ENTRYPOINT
19 Jan 2020 | 2 min read
CMD allows you to define the default command with params when the image gets executed in a container.
ENTRYPOINT allows you to specify a command or script that needs to be executed. Both can be used in conjunction to get them to specify the Entrypoint and default arguments.
This is a quick summary of the difference between CMD and ENTRYPOINT and to know when to use what.
In essence, when you execute:
docker run ubuntu
The docker container would run and exit immediately. This due to the fact that containers are expected to die out soon as it's done with what it's supposed to do. For instance, running the above command will execute
bash and immediately exit.
But, if you do the following:
docker run -it ubuntu top
This would result in the container running the
top command on the container and it will wait for us to exit the program so that it would exit.
So, in general, the container's lifespan is only until the task it's assigned to run completes.
The CMD command in docker, basically allows us to execute a command. Defining this at the end of the Dockerfile allows us to create an image that will execute that given command whenever the image is invoked.
FROM alpine CMD ["sleep", "10"]
This script would literally halt the container for 10 seconds before it exits.
We still have the ability to override the CMD command by specifying a command on the URL.
docker run name-of-the-image-built sleep 1
Entrypoint basically executes a command/script anything that you would specify and passes through whatever that you specify in the docker run command.
FROM alpine ENTRYPOINT ["sleep"]
When you build and run this Dockerfile, it will basically fail.
docker run alpine-with-entry-point
Because the sleep command expects 1 argument. So to get this to work, you would run:
docker run alpine-with-entry-point 10
Doing the above is essentially equal to saying
sleep 10 when the image gets run.
The downside with an Entrypoint is, that if your command needs an argument like in the example above, you need to specify it always. Not having would result in the container crashing with an error. To overcome this we can do the following:
FROM alpine ENTRYPOINT ["sleep"] CMD ["10"]
This would essentially make sure that 10 gets passed into the sleep command as a default argument if nothing is passed. Avoiding any crashes due to missing args.
Most of the content I have written about here are my notes from following the docker course I came across on the following video