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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github