Understanding the difference between RUN and CMD in a Dockerfile is very important for making good Docker images. RUN helps us run commands while we build the image. We can use it to install packages or set up the environment. CMD, on the other hand, tells us what command to run when we start a container from the image. Learning these two commands can really improve our Docker work and help us with containerized apps.
In this article, we will look at the small details of RUN and CMD in Dockerfiles. We will explain what they do and how we can use them. We will see how to use RUN to install what we need. We will also learn how to use CMD to set default commands. Plus, we will talk about when to pick one over the other. Also, we will answer some common questions to help clear up confusion for Docker users.
- What is the Difference Between RUN and CMD in a Dockerfile
- Understanding RUN in a Dockerfile
- Understanding CMD in a Dockerfile
- How to Use RUN to Install Dependencies
- How to Use CMD to Specify Default Commands
- When to Use RUN and When to Use CMD
- Frequently Asked Questions
What is the Difference Between RUN and CMD in a Dockerfile
In a Dockerfile, RUN and CMD have different
jobs. We use RUN to run commands while we build the image.
CMD tells us the default command that runs when we start a
container from the image.
Understanding RUN in a Dockerfile
We mainly use the RUN instruction to install software,
set up the environment, or do other tasks when we create the image. Each
RUN command makes a new layer in the Docker image. If we
are not careful, this can make the image bigger.
Example of RUN
FROM ubuntu:20.04
# Install the curl package
RUN apt-get update && apt-get install -y curlIn this example, RUN updates the package list and
installs the curl package. This creates a new image layer
that has curl.
Understanding CMD in a Dockerfile
The CMD instruction sets the default command that runs
when we start a container from the image. We can only have one
CMD instruction in a Dockerfile. If we write more, only the
last one will work.
Example of CMD
FROM ubuntu:20.04
# Set the default command to execute when the container starts
CMD ["echo", "Hello, Docker!"]In this example, when we run a container from the image, it will run
the command echo Hello, Docker!.
How to Use RUN to Install Dependencies
We can use RUN to install dependencies in one layer.
This helps to keep the image size smaller. It is common to use one
RUN instruction for installing many packages.
Example of Installing Multiple Packages
FROM node:14
# Install dependencies
RUN apt-get update && \
apt-get install -y git && \
npm install expressThis example updates the package list. It installs git
and the express package all in one layer.
How to Use CMD to Specify Default Commands
We can use CMD in two ways: exec form or shell form. We
recommend using exec form. It avoids the shell and helps handle signals
better.
Example of CMD in Exec Form
FROM python:3.9
# Set the default command to run a Python script
CMD ["python", "app.py"]In this example, CMD runs the app.py script
when the container starts.
When to Use RUN and When to Use CMD
- Use RUN when we need to install software or set up the environment during the build process.
- Use CMD to tell what command should run when we start a container from the built image.
This helps us to keep building the image separate from running the container.
Frequently Asked Questions
Q: Can I use both RUN and CMD in the same
Dockerfile?
A: Yes, we can use both. RUN is for building the image.
CMD is for running the container.
Q: What happens if I specify multiple CMD
instructions?
A: Only the last CMD instruction will work. The earlier
ones are ignored.
For more insights on Docker and its parts, you can check what is Docker and why should you use it.
Understanding CMD in a Dockerfile
In a Dockerfile, the CMD command tells what should run
by default when we start a container from the image. This is different
from RUN, which runs commands when we build the image.
CMD shows how the container acts when it is running.
Syntax
There are three ways to write the CMD command:
Exec form (this is the best way):
CMD ["executable","param1","param2"]Example:
CMD ["nginx", "-g", "daemon off;"]Shell form:
CMD command param1 param2Example:
CMD nginx -g 'daemon off;'Default parameters for
ENTRYPOINT:CMD ["param1", "param2"]We use this way with the
ENTRYPOINTcommand.
Behavior
We can change the
CMDcommand by giving a different command when we run the container. For example:docker run myimage mycommandWe can only have one
CMDcommand in a Dockerfile. If we write more than one, only the last one will work.We often use
CMDto set the main thing that the container runs like starting a web server or running a script.
Best Practices
- It is better to use the exec form to avoid problems with signals.
- We should place the
CMDcommand at the end of the Dockerfile after allRUNcommands and settings. - Use
CMDfor default commands. UseENTRYPOINTfor running commands when we need it.
For more information on Docker commands and best practices, you can check what is a Docker image and how is it different from a container.
How to Use RUN to Install Dependencies
In a Dockerfile, we use the RUN instruction to run
commands when we build the image. This is often used to install
dependencies and set up the environment. Each RUN
instruction makes a new layer in the image.
To install dependencies using the RUN command, we
usually use a package manager. Here are some examples for different
package managers:
Example for Ubuntu (using
apt-get)
FROM ubuntu:20.04
RUN apt-get update && \
apt-get install -y \
curl \
git \
build-essentialExample for Alpine (using
apk)
FROM alpine:latest
RUN apk add --no-cache \
curl \
git \
build-baseExample for Node.js (using
npm)
FROM node:14
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm installExample for Python (using
pip)
FROM python:3.9
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txtMultiple Commands in a Single
RUN
We can combine many commands in one RUN instruction to
reduce the number of layers:
RUN apt-get update && \
apt-get install -y \
curl \
git && \
apt-get cleanUsing RUN to install dependencies makes sure that our
Docker image has everything it needs to run our application when the
container starts. This way is very important for making environments
that we can repeat. We should use it well in our Dockerfile to make the
build work better.
How to Use CMD to Specify Default Commands
In a Dockerfile, we use the CMD instruction to set the
default command that runs when we start a container from the image. This
is different from RUN. RUN executes commands
when we build the image. But CMD sets the command that runs
when we launch the container.
Syntax
There are three ways to write the CMD instruction:
Exec form (we recommend this):
CMD ["executable", "param1", "param2"]Shell form:
CMD command param1 param2Default parameters for ENTRYPOINT:
CMD ["param1", "param2"]
Examples
Example 1: Using Exec Form
FROM ubuntu:latest
CMD ["echo", "Hello, World!"]This command will show “Hello, World!” when the container starts.
Example 2: Using Shell Form
FROM ubuntu:latest
CMD echo "Hello, World!"This also shows “Hello, World!” but it runs in a shell.
Example 3: Setting Default Parameters for ENTRYPOINT
FROM ubuntu:latest
ENTRYPOINT ["top", "-b"]
CMD ["-c"]Here, CMD gives default options to the
ENTRYPOINT. The container will run
top -b -c.
Important Notes
- We can only have one
CMDinstruction in a Dockerfile. If we put more than one, only the last one will work. - If we run the container with a command, that command will replace
the
CMDin the Dockerfile. - Use
CMDwhen we want to set defaults that we can change easily when the container runs.
For more details on Dockerfile instructions, check this article on what is a Dockerfile and how do you create one.
When to Use RUN and When to Use CMD
In a Dockerfile, it is very important to know when to use
RUN and when to use CMD. This helps us to
build good images and set how containers behave.
RUN
- We use
RUNto run commands while we build the image. This is good for installing software or changing the filesystem. - Each command we run creates a new layer in the Docker image.
Example:
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
python3 \
python3-pipCMD
- We use
CMDto set the default command that runs when we start a container from the image. - We can change this by adding arguments to
docker run. If we have manyCMDinstructions, only the last one will work.
Example:
CMD ["python3", "app.py"]Choosing Between RUN and CMD
- Use
RUNwhen we need to do tasks that build the image, like installing dependencies. - Use
CMDto set the default behavior when we run the container, like starting an application.
By knowing these differences, we can organize our Dockerfile well. This helps to make image build times faster and the container work better. For more tips on Docker best practices, check out this article.
Frequently Asked Questions
1. What is the primary purpose of the RUN command in a Dockerfile?
We use the RUN command in a Dockerfile to run commands
while we build the image. It helps us install packages, set up the
environment, or make files that will be in the final Docker image.
Knowing how to use the RUN command well is very important
to make Docker image builds better.
2. How does CMD differ from RUN in a Dockerfile?
The CMD command in a Dockerfile tells us the default
command that will run when a container starts from the image we built.
Unlike RUN, which runs commands when we build,
CMD is for when the container runs. We can change
CMD with command-line arguments. This difference is
important for using Docker to containerize applications.
3. Can I use both RUN and CMD in the same Dockerfile?
Yes, we can use both RUN and CMD in the
same Dockerfile. We use RUN to install dependencies or set
up the environment while we build the image. Then, CMD
tells us what command to run when the container starts. This way, we can
have a flexible Dockerfile that builds and runs applications
correctly.
4. When should I use RUN instead of CMD in my Dockerfile?
We should use RUN when we need to install packages or do
setup tasks during the image build. On the other hand, we use
CMD to say what command should run when the container is
up. This difference helps us keep our Dockerfile organized for both
making images and running containers.
5. What happens if I use CMD and ENTRYPOINT together in a Dockerfile?
When we use CMD and ENTRYPOINT together in
a Dockerfile, CMD gives default arguments to the
ENTRYPOINT. This way, we can set a main command with
ENTRYPOINT and use CMD to give arguments that
we can change when we run it. This mix gives us more choices on how our
Docker container works. For more details on Docker commands, check out
this
article on the key differences between CMD and ENTRYPOINT.