Sub topic 1
What a Dockerfile does: images, containers, and layers
Prerequisites
This chapter assumes you can use a terminal and have Docker installed, so that docker version runs without errors. You should know in general terms what a container is. You should also be able to run a small program, such as a Node.js script, on your own machine. No earlier Docker experience is needed.
The three objects
Three things are easy to confuse when you start with Docker.
- A Dockerfile is a text file of instructions. It describes how to build an image.
- An image is a read-only package. It holds a filesystem (the files your app needs) and metadata, such as the default command and environment variables.
- A container is a running instance of an image. It is an isolated process with its own filesystem view, created from the image plus a thin writable layer.
The relationship is one-directional. A Dockerfile is the source, and docker build turns it into an image. docker run turns the image into a container. You can start many containers from one image, and none of them changes the image.
Layers
An image is not one big file. It is a stack of layers. A layer is a set of filesystem changes, such as files added or removed. Instructions that change the filesystem, like RUN and COPY, each create a layer. Instructions that only set metadata, like ENV, CMD, and EXPOSE, add no files and so produce no meaningful filesystem change.
Layers matter for three reasons:
- Caching. Docker reuses a layer from an earlier build if the instruction and its inputs have not changed.
- Sharing. Images that start from the same base share its layers on disk and during a pull.
- Size. Layers only add up. If one instruction downloads a large file and a later instruction deletes it, the file is still stored in the earlier layer.
When a container starts, Docker adds a thin writable layer on top of the read-only image layers. Anything the container writes goes there and is lost when the container is removed.
A first example
This Dockerfile builds an image for a Node.js app. The later chapters explain each instruction in detail. For now, notice that each line is one step.
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]Reading from the top:
FROMpicks the starting image. Every layer after it builds on this one.COPYandRUNchange the filesystem, so each adds a layer.ENV,EXPOSE,USER, andCMDrecord settings in the image metadata.
Build and run it:
docker build -t myapp:1.0 .
docker run --rm -p 3000:3000 myapp:1.0The first command reads the Dockerfile in the current directory and produces an image tagged myapp:1.0. The second starts a container from that image. -p 3000:3000 publishes container port 3000 on the host. --rm deletes the container when it exits.
To see the layers of the image you built, run docker history myapp:1.0. Each row is a layer or a metadata step, with its size.
Common mistakes
- Treating a container as permanent storage. Data written inside a container disappears with it. Use a volume for data you need to keep.
- Editing a running container to "fix" it. Changes made this way are not in the image. Change the Dockerfile and rebuild instead.
- Deleting files in a later layer to save space. The earlier layer still holds them, so the image does not shrink. Avoid adding the files in the first place, or remove them in the same
RUNinstruction that created them. - Mixing up build time and run time.
RUNexecutes while the image is built.CMDexecutes when a container starts.
Exercise
You build an image from a Dockerfile and start three containers from it. One container creates a file under /tmp. Does that file appear in the other two containers or in the image? Explain why.
Show answer
No. The file is written to that container's own thin writable layer, which is separate from the read-only image layers and from other containers. The image and the other two containers are unchanged. The file is also lost when that container is removed.
Exercise
A Dockerfile has one instruction, RUN curl -o big.tar https://example.com/big.tar, followed by a second instruction, RUN rm big.tar. Why is the final image still large, and how would you avoid that?
Show answer
Each RUN creates its own layer. The first layer stores big.tar, and the second layer only marks it as deleted, so the data is still in the image. Combine the download, use, and removal in a single RUN instruction so the file never persists in any layer.