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

Better Stack Team
Updated on October 5, 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.