Sub topic 1
How a Dockerfile becomes an image
A Dockerfile is a plain text file of instructions. docker build reads it from top to bottom and produces an image. This chapter shows that process end to end, so the later chapters on individual instructions have a place to attach.
Prerequisites
This chapter assumes you have the following:
- Basic command line use.
- A working idea of what a container and an image are.
- Docker installed, with
docker buildanddocker runworking.
The working model
An image is a stack of read-only layers plus metadata, such as the default command. Each instruction in a Dockerfile does one of two things:
- It changes the filesystem, and Docker records the change as a new layer.
RUNandCOPYdo this. - It changes only metadata, and no files change.
CMD,ENTRYPOINT, andWORKDIRdo this.
The build runs the instructions in order. Each step starts from the result of the previous step. When the last instruction finishes, Docker tags the final result as your image.
A first Dockerfile
Create a directory with two files. The first is app.py:
print("hello from the container")The second is Dockerfile:
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]Each line has a role:
FROMpicks the base image. Every Dockerfile starts from one, and everything else builds on it.WORKDIRsets the directory for the instructions that follow, and creates it if needed.COPYcopiesapp.pyfrom your machine into/appin the image.CMDrecords the command to run when a container starts. It does not run during the build.
Building and running
Run the build from the directory that holds the Dockerfile:
docker build -t hello-app .
docker run --rm hello-appThe -t hello-app option tags the image with a name. The final . is the build context, which is the set of files Docker may copy from. The second command starts a container from the image and prints hello from the container. The --rm option deletes the container when it exits.
The build prints one step per instruction. Running the same build again shows steps marked as cached, because Docker reuses layers whose inputs have not changed. Later chapters cover that cache in detail.
Build time versus run time
Two moments are easy to confuse:
| Moment | What happens | Instructions involved |
|---|---|---|
| Build time | Docker executes steps and stores layers | `FROM`, `RUN`, `COPY` |
| Run time | A container starts and runs one process | `CMD`, `ENTRYPOINT` |
RUN pip install flask happens once, during the build, and its result is stored in the image. CMD only describes what to start later. Mixing these up is the most common early mistake. A command you want to run on every container start belongs in CMD or ENTRYPOINT, not in RUN.
Common errors
- "unable to prepare context: path not found" or a missing Dockerfile. You ran
docker buildfrom the wrong directory, or the path argument is wrong. Check where the Dockerfile is. - "COPY failed: file not found in build context". The file is outside the context directory, or a
.dockerignorerule excludes it. Docker cannot copy anything from outside the context. - Instructions in the wrong order.
FROMmust come first, apart fromARGlines that feed it. Other instructions placed before it fail the build.
Exercise
In the Dockerfile above, which instruction runs when you execute docker build, and which one runs when you execute docker run? Does CMD ["python", "app.py"] print anything during the build?
Show answer
FROM, WORKDIR, and COPY take effect during docker build. CMD is stored as image metadata and runs only at docker run. It prints nothing during the build, so the text "hello from the container" appears only when a container starts.