RUN and CMD both are important instructions in a Dockerfile. They seem to be similar but they are different.

Difference between run and cmd in Dockerfile

RUN in a Dockerfile

RUN is executed at build time. It means that when you build a docker image, RUN gets executed.

Each RUN command creates a new layer inside the docker image.

When you want to install / build dependencies, You will use RUN.


RUN apt-get update
RUN apt-get install curl wget

RUN mkdir -p /usr/src/app

RUN npm install

RUN npm clean

RUN pip install -r requirements.txt

You can also combine multiple run commands to avoid creating multiple layers in the docker image.


RUN apt-get update && apt-get install curl wget
RUN mkdir -p /usr/src/app
RUN npm install && npm clean

CMD in a Dockerfile

CMD lets you define a default command to run when your container starts. CMD is executed at run-time. It means when you launch a container using a docker image.

To launch or run application process in a docker image Use CMD. Starting the web server is the most commonly used command.

CMD npm run dev	
CMD node app.js
CMD python main.py