Sub topic 5
RUN: executing commands while building
This chapter assumes you can read a basic Dockerfile and run docker build. It also assumes you know that an image is made of layers. RUN executes a command at build time and saves the result into the image.
What RUN does
RUN starts a temporary container from the image built so far. It runs your command inside that container and commits the changes to the filesystem as a new layer. The command runs only during docker build. It does not run when a container starts. That job belongs to CMD and ENTRYPOINT, which later chapters cover.
A basic example
FROM python:3.12-slim
RUN pip install --no-cache-dir requests
RUN python -c "import requests; print(requests.__version__)"The first RUN installs a package. The second checks that the install worked. If any RUN command exits with a non-zero status, the build stops and reports the error.
Shell form and exec form
RUN accepts two forms.
- Shell form:
RUN apt-get update. Docker runs it as/bin/sh -c "apt-get update"on Linux. Pipes,&&, and variable expansion work. - Exec form:
RUN ["apt-get", "update"]. Docker runs the program directly, with no shell. The value must be a JSON array, so use double quotes.
Use shell form for most RUN lines, because build steps usually chain commands. Use exec form when the image has no shell or when you need a different one:
FROM ubuntu:24.04
RUN ["/bin/bash", "-c", "echo $BASH_VERSION"]Combine related commands
Each RUN creates a layer. Files deleted in a later layer still exist in the earlier one, so the image does not get smaller. Package manager caches are the usual example.
This version keeps the cache in the image:
FROM debian:12-slim
RUN apt-get update
RUN apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*This version cleans up in the same layer, so the cache never lands in the image:
FROM debian:12-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*Running apt-get update in its own RUN also causes a cache problem. Docker may reuse that layer later and install from a stale package list. Keep update and install in one instruction.
Common errors
- Failures hidden by pipes. In
RUN curl -s example.com/x | sh, the shell reports only the last command's status. Ifcurlfails, the build can still pass. In shells that support it, addset -o pipefailfirst. - Interactive prompts. There is no terminal during a build. Use flags like
-yso commands do not wait for input. - Expecting
cdto persist. EachRUNstarts fresh. UseWORKDIRto change directory for later instructions. - Expecting
RUNto start a service. The temporary container exits when the command finishes. Background processes do not carry over.
Checking the result
After a build, docker history <image> lists each layer with the command that made it and its size. Use it to find a RUN that added more data than you expected.
Exercise
A Dockerfile has RUN apt-get install -y build-essential followed by RUN rm -rf /var/lib/apt/lists/*. Why does the second line fail to shrink the image, and how do you fix it?
Show answer
Each RUN creates its own layer, and the files from the first layer remain in the image even after a later layer deletes them. Combine the install and the cleanup in one RUN with &&, for example RUN apt-get update && apt-get install -y build-essential && rm -rf /var/lib/apt/lists/*.
Exercise
Why does RUN cd /app followed by RUN ls not list the contents of /app?
Show answer
Every RUN starts a new temporary container with the default working directory, so the cd does not carry over. Use WORKDIR /app instead.