Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #36: The Difference between RUN and CMD in a Dockerfile

docker-tips-and-tricks.jpg

RUN lets you run commands but CMD also lets you run a command, so which one should you use, and when?

Quick Jump:

While you can run commands with both instructions, they are very different.

RUN happens at build time. When you build your Docker image, Docker will read in your RUN command and build it into your image as a separate image layer.

CMD happens at run time. This will likely be calling some type of process, such as nginx, bash or whatever process your Docker image runs. This does not create a separate image layer.

Your Dockerfile may have many RUN instructions but will have at most one CMD.

Which one should you use and when?
  • Are you building dependencies into your image? Use RUN
  • Are you launching / running your Dockerized process? Use CMD

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments