What is the difference between RUN and CMD in a Dockerfile?

Better Stack Team
Updated on April 7, 2023

RUN and CMD are both instructions used in Dockerfiles, but they have different purposes.

RUN instruction

RUN is used to execute commands during the build process of a Docker image. These commands are run in a new layer on top of the current image and their result is saved in the new image layer. The commands specified with RUN are typically used to install software packages, update system configurations, create directories, and perform other tasks that are necessary to configure the image.

CMD instruction

CMD, on the other hand, is used to specify the default command to run when a Docker container is started from the image. This command is only executed when the container is started and it can be overridden by passing a different command to the docker run command line. CMD is typically used to start a service or application in the container.

To summarize, RUN is used to execute commands during the build process of a Docker image, while CMD is used to specify the default command to run when a Docker container is started from the image.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack