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.
This is a run-time operation, but you still need to re-build your Docker image to change what your
CMD does. A running image is called a container btw.
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.