124k views
4 votes
What is difference between run CMD and ENTRYPOINT in docker?

User ManuQiao
by
7.9k points

1 Answer

0 votes

Final answer:

The RUN instruction is used during the Docker image build process to execute commands, while CMD sets a default command for container startup that can be overridden. ENTRYPOINT also sets a container command but is not easily overridden, allowing the container to run as an executable.

Step-by-step explanation:

Difference Between RUN CMD and ENTRYPOINT in Docker

The RUN, CMD, and ENTRYPOINT instructions in a Dockerfile are designed to specify what should be executed in a Docker container. Although they are similar in purpose, their behavior and use cases differ significantly.

The RUN instruction is used to execute commands during the image build process. It creates a new layer on top of the existing image after the execution of the command and commits the results. This means any changes made by the RUN command affect the resulting Docker image. It's often used for installing packages, compiling code, or setting up the file system.

The CMD instruction specifies the default command to run when a container starts. Unlike RUN, it does not execute anything during the image build process but sets the default command that is executed when the container starts. If the CMD command is specified multiple times, the last one takes effect.

The ENTRYPOINT instruction also specifies a command that will always be executed when the container starts. The key difference is that with ENTRYPOINT, the command and parameters are not overwritten easily from the command line arguments. If both ENTRYPOINT and CMD are specified, the CMD instruction will provide default parameters to the ENTRYPOINT command.

Choosing between CMD and ENTRYPOINT depends on whether you need your command to be overridden easily. CMD is suitable for setting a default that users can easily override, while ENTRYPOINT is for configuring a container that will run as an executable.

User Brad Albright
by
7.1k points