Sub topic 1
Images and containers: what a build produces
A Docker build produces an image. Running that image produces a container. This chapter separates the two, because the rest of the tutorial depends on knowing which one holds what.
Prerequisites
This chapter assumes you can:
- Run
docker build,docker run, anddocker pull. - Read and write a short Dockerfile.
- Use a terminal.
What a build produces
A Dockerfile is a text recipe. docker build reads it, runs each instruction in order, and saves the result as an image. An image is a read-only package containing a filesystem, plus metadata such as the default command, environment variables, and exposed ports.
An image never changes after it is built. A new build creates a new image, even if the Dockerfile is nearly identical.
The directory you pass to docker build is the build context. The builder can only copy files from the context into the image. A later chapter covers how to keep it small.
What a container adds
A container is a running instance of an image. When Docker starts one, it does three things:
- It takes the image's filesystem as a read-only base.
- It adds a thin writable layer on top, unique to that container.
- It starts the image's default command as a process, unless you override it.
Every file the container creates, edits, or deletes goes into the writable layer. The image underneath is untouched. Many containers can start from one image, and each gets its own writable layer.
Worked example
Create a Dockerfile:
FROM alpine:3.20
RUN echo "written at build time" > /greeting.txt
CMD ["cat", "/greeting.txt"]Build the image, run it, then change the file inside a second container:
docker build -t greeter:1 .
docker run --rm greeter:1
docker run -d --name c1 greeter:1 sleep 300
docker exec c1 sh -c 'echo "changed in container" > /greeting.txt'
docker exec c1 cat /greeting.txt
docker run --rm greeter:1
docker rm -f c1The output shows three results:
- The first run prints
written at build time. - Inside
c1, the file readschanged in container. - A fresh container from the same image prints
written at build timeagain.
The edit lived only in the writable layer of c1. The image was never modified.
Why this matters for builds
Two facts from this chapter carry through the rest of the tutorial:
- Images are immutable. Nothing a build step does later can edit an earlier step's result. It can only add to it. This explains why deleting a file in a later step does not shrink the image.
- Containers are cheap. A container shares the image's storage and adds only its own changes. Starting ten containers does not copy the image ten times.
The next chapter looks inside the image and shows that it is a stack of layers, and that each build step contributes one.
Exercise
You start two containers, A and B, from the same image. Inside A you delete /etc/hostname-backup, a file that came from the image. What does B see, and what does the image contain afterward?
Show answer
B still sees /etc/hostname-backup, and the image still contains it. A's deletion is recorded only in A's own writable layer. The image is read-only and shared, and B has a separate writable layer.