What is the difference between CMD and ENTRYPOINT?
sobes.tech AI
Answer from AI
In a Dockerfile, CMD and ENTRYPOINT specify commands to run the container, but they have different purposes and behaviors.
-
CMD — sets the default command, which can be overridden when running the container. It is usually used to specify arguments or a command if none are provided.
-
ENTRYPOINT — sets the main command that always executes when the container starts. Arguments passed at runtime are added to
ENTRYPOINT.
Example:
ENTRYPOINT ["/bin/echo"]
CMD ["Hello, World!"]
When running docker run image, /bin/echo Hello, World! will be executed.
If you run docker run image Goodbye, /bin/echo Goodbye will be executed.
If only CMD is used, then the command is completely replaced when running with arguments.
Thus, ENTRYPOINT fixes the command, and CMD sets the default arguments.