Skip to content

CMD

CMD ["echo", "Hello World"]
or
CMD echo "Hello World"
echo - command Hello World - command arguments

When starting a Docker container, you can override the CMD command:

docker run cmd:1.0 echo "new message"
docker run cmd:1.0 printenv
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.

ENTRYPOINT ["echo"]
CMD ["Hello World"]

docker run cmd:1.0 printenv CMD ["Hello World"] will be overridden and the terminal output will be:

printenv
(just a word as an argument for echo)

FROM alpine:3.18
ENTRYPOINT ["echo", "Hello"]
CMD ["Evgeniy"]
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:

  • ENTRYPOINT and CMD can be used to start the main process in a container. The CMD command can be overridden, while the ENTRYPOINT command cannot — you can only set additional parameters for it.
  • Use ENTRYPOINT when writing a Dockerfile that should run as a program, and when you don't want the main command to be overridable.
  • Use CMD to set default additional parameters for the ENTRYPOINT command. This way the Dockerfile user can override them when needed.