CMD
or echo - command Hello World - command argumentsWhen starting a Docker container, you can override the CMD command:
Here echo "Hello World" will not be executed. Also, if the Dockerfile itself contains multiple CMD instructions, only the last one will be executed.ENTRYPOINT
Usually specified in the Dockerfile and not changed (only for testing purposes it can be changed in docker run using the --entrypoint flag). ENTRYPOINT is used more for launching a specific program, while CMD is used for arguments.
docker run cmd:1.0 printenv
CMD ["Hello World"] will be overridden and the terminal output will be:
docker run cmdenv:1.0 Dear Student
Will output
Hello Dear Student
With the ENTRYPOINT instruction we defined a command and a parameter for it that will be executed at container startup — echo Hello. This command cannot be overridden. We also set an additional parameter that can be overridden when starting the container.
Summary:
ENTRYPOINTandCMDcan be used to start the main process in a container. The CMD command can be overridden, while theENTRYPOINTcommand cannot — you can only set additional parameters for it.- Use
ENTRYPOINTwhen writing aDockerfilethat should run as a program, and when you don't want the main command to be overridable. - Use
CMDto set default additional parameters for theENTRYPOINTcommand. This way theDockerfileuser can override them when needed.