Sub topic 2
Writing a Dockerfile: FROM, WORKDIR, COPY, RUN
This chapter assumes you can run docker run, use a terminal, and know what a container is. You will write a Dockerfile for a small Python app and learn what each of the four core instructions does to the image.
What a Dockerfile is
A Dockerfile is a text file of instructions that docker build reads from top to bottom. Most instructions add a layer to the image. Each line has the form INSTRUCTION arguments. By convention instructions are written in uppercase.
A minimal example
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]The sections below walk through the first four instructions in this file. CMD sets the default command and gets its own chapter later.
FROM: choose the base image
FROM sets the starting point. Every later instruction builds on it. Here python:3.12-slim is an image from Docker Hub with Python 3.12 and a reduced set of system packages.
- A Dockerfile must begin with
FROM. The only instructions allowed before it areARGand comments. - Name a specific tag such as
3.12-slim. Without a tag you getlatest, which can change under you. - Smaller bases like
-slimdownload faster but have fewer tools installed.
WORKDIR: set the working directory
WORKDIR /app sets the directory for the instructions that follow it. It affects RUN, COPY, CMD, and ENTRYPOINT. It also becomes the starting directory when the container runs.
If the directory does not exist, Docker creates it. A relative path is resolved against the previous WORKDIR. Use WORKDIR instead of RUN cd /app, because a cd inside RUN applies only to that one instruction.
COPY: bring files into the image
COPY <source> <destination> copies files from the build context into the image. The source is relative to the build context. The destination is relative to WORKDIR unless it starts with /.
In the example, COPY requirements.txt . copies one file into /app. COPY . . copies everything else from the context into /app. The two are separate on purpose, which the caching chapter explains.
RUN: execute a command at build time
RUN runs a command inside the image during the build and saves the result as a new layer. Use it to install packages or compile code. RUN happens once at build time. It does not run when a container starts.
RUN has two forms:
| Form | Example | Behavior |
|---|---|---|
| Shell form | `RUN pip install flask` | Runs through `/bin/sh -c`, so shell features like `&&` work |
| Exec form | `RUN ["pip", "install", "flask"]` | Runs the program directly, with no shell |
Chain related commands with && so they share one layer:
RUN apt-get update && apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*Deleting files in a later RUN does not shrink the image, because earlier layers keep them. Clean up in the same instruction that created the files.
How the instructions become layers
Common errors
COPY failed: file not found in build context: the source path is wrong, or.dockerignoreexcludes it. Paths cannot point outside the context, soCOPY ../x .fails.pip: command not found: the base image lacks the tool. Pick an image that includes it, or install it first.- Copying
requirements.txtafterCOPY . .: this works, but it reinstalls dependencies on every code change.
Exercise
In the example Dockerfile, why is WORKDIR /app preferable to writing RUN cd /app before the COPY instructions?
Show answer
RUN cd /app changes the directory only for that one RUN instruction, so later instructions do not stay in /app. WORKDIR /app applies to all following instructions and to the container's starting directory, and Docker creates the directory if it is missing.
Exercise
A Dockerfile has RUN apt-get install -y curl in one instruction and RUN rm -rf /var/lib/apt/lists/* in the next. Why does the cleanup fail to reduce image size, and how do you fix it?
Show answer
The first RUN already saved the package lists in its layer, and a later layer cannot remove data from an earlier one. Combine both commands in a single RUN joined with && so the files are deleted before the layer is saved.