Difference between RUN and CMD in a Dockerfile
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding the Difference Between RUN and CMD in a Dockerfile
In Docker, a Dockerfile is a text document that contains all the commands you execute to assemble an image. It provides the instruction set through which a Docker image is built. Two fundamental instructions that often create confusion among developers, especially those new to Docker, are RUN and CMD. While both are essential for building and running Docker images, they serve different purposes. Let's delve into these two instructions to understand their distinctions better.
RUN Instruction
The RUN command is used to execute commands in a new layer on top of the existing image and commit the results. It generates a new image layer as part of a Docker image build process.
Key Points About RUN:
- Purpose: To build up an image by installing packages, dependencies, setting environment variables and updating the operating system.
- Execution Time: During the build process of a Docker image.
- Creates a New Layer: Each
RUNinstruction creates an intermediate layer. As a result, it helps achieve caching benefits. - Multiple Commands: Although you can chain multiple commands using shell syntax (
&&), it's common to have single self-contained commands.
Example:
In the above example, the RUN instruction updates the package list and installs Python and the Flask package. Each RUN produces a distinct layer that is cached and boosts successive builds' performance if there are no changes to the respective steps.
CMD Instruction
Unlike RUN, the CMD instruction specifies the default command to execute when a container is started. Only the last CMD instruction in the Dockerfile is considered, and it can be overridden by arguments provided in the docker run command.
Key Points About CMD:
- Purpose: To provide the default command or entrypoint for an executing container.
- Execution Time: During the lifecycle of a container (not during the build).
- Does Not Create a Layer: Unlike
RUN,CMDdoes not create image layers. - Override Behavior: Can be overridden at runtime by passing a different command.
Types of CMD:
- Shell Form:
CMD command param1 param2, executed in/bin/sh -c, suitable for simple commands. - Exec Form:
CMD ["command", "param1", "param2"], executed directly, more suited for subprocess control, handling signals.
Example:
In this case, the CMD specifies that python3 /myscript.py should be executed by default when the container starts. If someone runs docker run <image> bash, it will override this CMD instruction.
Key Differences in a Summary Table
Here's a summary of the main differences between RUN and CMD:
| Feature | RUN | CMD |
| Purpose | Builds the image by installing software and configuring the environment. | Specifies defaults for executing the container. |
| Execution Time | During image build. | During container lifecycle. |
| Creates New Layer | Yes, each RUN creates a new layer. | No, it doesn't create additional image layers. |
| Effect on Image/Container | Modifies the image itself. | Affects container behavior upon start. |
| Overridable | N/A | Yes, by passing command in docker run. |
| Best Used | When installing and configuring software within the image. | For defining the container's default running state. |
Additional Considerations
- Chaining RUN Commands: Minimizing layers by chaining multiple commands with
&&can shrink image size, but might obfuscate individual steps in the Dockerfile. - Combining with ENTRYPOINT: In scenarios requiring a particular application to be executed with specific parameters consistently, leveraging
ENTRYPOINTwithCMDallows specifying fixed and overridable parts. - Efficiency in Rebuilding: Efficient use of
RUNinstructions can leverage Docker's cache layers, leading to faster rebuilds.
Conclusion
Understanding the distinction between RUN and CMD is crucial when authoring Dockerfiles because they affect the image-building process and container runtime behavior differently. RUN focuses on preparing the image, while CMD sets up the default environment for container execution. By leveraging each appropriately, developers can optimize both their Docker images and container executions.

