Intern
What is the difference between CMD and ENTRYPOINT in Docker?
sobes.tech AI
Answer from AI
In Docker, CMD and ENTRYPOINT specify commands that will be executed when the container starts, but they serve different purposes and behaviors:
ENTRYPOINTsets the main command of the container, which will always be executed. It defines what the container does.CMDsets default arguments forENTRYPOINTor a command ifENTRYPOINTis not specified.
If the Dockerfile contains only CMD, then the command can be overridden at runtime by specifying a different one in docker run. If ENTRYPOINT is set, then the command from docker run will be passed as arguments to ENTRYPOINT.
Example:
ENTRYPOINT ["echo"]
CMD ["Hello, world!"]
Running docker run without arguments will output Hello, world!, while docker run <image> Bye will output Bye.
Thus, ENTRYPOINT fixes the main command, and CMD sets its default parameters.