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 #7: The Difference between RUN and CMD in a Dockerfile

blog/cards/docker-tips-and-tricks.jpg

RUN and CMD are very important pieces to a Dockerfile and they both perform much different tasks. Here's what they do.

RUN and CMD are both Dockerfile instructions.

RUN lets you execute commands inside of your Docker image. These commands get executed once at build time and get written into your Docker image as a new layer.

For example if you wanted to install a package or create a directory inside of your Docker image then RUN will be what you’ll want to use. For example, RUN mkdir -p /path/to/folder.

CMD lets you define a default command to run when your container starts.

You could say that CMD is a Docker run-time operation, meaning it’s not something that gets executed at build time. It happens when you run an image. A running image is called a container.

For example, if you were creating a Dockerfile for your own web application, a reasonable CMD to have would be to start your web application’s app server.

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