Sobes.tech
Junior — Middle

How to distinguish between CMD and ENTRYPOINT instructions in a Dockerfile and what are their main purposes?

sobes.tech AI

Answer from AI

CMD and ENTRYPOINT are instructions in a Dockerfile that specify the command executed when the container starts, but they have different purposes and behaviors.

  • ENTRYPOINT sets the main command of the container, which will always be executed. It defines what the container does by default and usually does not change when starting the container.
  • CMD sets default arguments for ENTRYPOINT or, if ENTRYPOINT is not specified, becomes the default command.

If arguments are passed when starting the container:

  • With ENTRYPOINT, these arguments are added to the ENTRYPOINT command.
  • With only CMD, the arguments replace CMD.

Example:

ENTRYPOINT ["echo"]
CMD ["Hello, world!"]

Running docker run image will output "Hello, world!", and docker run image Goodbye will output "Goodbye".

Thus, ENTRYPOINT fixes the main command, and CMD sets its default parameters.