Docker Tip #7: The Difference between RUN and CMD in a Dockerfile
RUN and CMD are very important pieces to a Dockerfile and they both perform much different tasks. Here's what they do.
CMD are both
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.