Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Writing a Dockerfile

By the end of this chapter, you'll be able to

  • Write a Dockerfile with FROM, WORKDIR, COPY, RUN, EXPOSE and CMD
  • Build an image with docker build and run it
  • Order the instructions to benefit from the layer cache
  • Exclude useless files with .dockerignore and avoid running as root

A first Dockerfile

Let's take a small Python web application (with the Flask framework): an app.py file and a requirements.txt file listing its dependencies.

# 1. The starting image: Python 3.13 on a reduced Debian base
FROM python:3.13-slim

# 2. The working directory in the image (created if it does not exist)
WORKDIR /app

# 3. Install the dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 4. Copy the application's code
COPY . .

# 5. Document the port listened on, then the start-up command
EXPOSE 5000
CMD ["python", "-m", "flask", "--app", "app", "run", "--host", "0.0.0.0", "--port", "5000"]

The instructions, one per line:

  • FROM: the base image, always first;
  • WORKDIR: the current directory for the following instructions and at start-up;
  • COPY source destination: copy files from your project into the image;
  • RUN: run a command during the build (install packages);
  • EXPOSE: state the port listened on (it is documentation: -p really publishes);
  • CMD: the command run when the container starts.

We build the image then run it:

docker build -t library:1.0 .
docker run --rm -p 5000:5000 library:1.0

-t gives the image a name and a tag, and the final . designates the context: the folder sent to Docker for the build. The server listens on 0.0.0.0 and not on 127.0.0.1, otherwise it would only be reachable from inside the container.

Layers and the cache

Each Dockerfile instruction produces a layer of the image. Docker keeps these layers in a cache: if an instruction and everything before it have not changed, it reuses the layer instead of redoing it. As soon as a layer changes, all the following ones are rebuilt.

Only app.py changed: only the last copy is redone. The build takes a second.

Step 5 of 5
Text description of the animation
  1. The base image has not changed: layer taken from the cache.
  2. Same instruction, same starting point: cache.
  3. requirements.txt has not changed: cache.
  4. Installing the dependencies, the slowest step, is taken from the cache.
  5. Only app.py changed: only the last copy is redone. The build takes a second.

This is why we copy requirements.txt alone, before the rest of the code: dependencies rarely change, code often does.

A safer, smaller image

Three good habits:

1. A .dockerignore file, next to the Dockerfile, so that what has no business in the image is not sent:

.git
__pycache__/
*.pyc
.env
venv/
node_modules/

The .env, which often contains passwords, must never end up in an image: anyone who gets the image can read its files.

2. Don't run as root. By default, the container's process runs as root, the administrator. If the application is hacked, the attacker has every right in the container. We create an ordinary user:

RUN useradd --create-home appuser
USER appuser

3. A reduced base. python:3.13-slim weighs a few hundred MB less than python:3.13. Less software also means fewer possible vulnerabilities.

Your turn

Worked exercise

Worked exercise: a Node.js API's Dockerfile

Write the Dockerfile of a Node.js API (the one from the APIs course): base node:24-slim, folder /app, dependencies listed in package.json and package-lock.json and installed with npm ci --omit=dev, code in server.js, port 3000, non-root user. The dependencies must benefit from the cache.

Hint 1
First copy package.json and package-lock.json, install, then copy the rest.
Hint 2
The official node images already contain an ordinary user named node.
Show the solution and common mistakes
FROM node:24-slim
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci --omit=dev

COPY . .

USER node
EXPOSE 3000
CMD ["node", "server.js"]

And the matching .dockerignore:

node_modules/
.git
.env

npm ci installs exactly the versions in package-lock.json, which makes the build reproducible; --omit=dev leaves out development tools (tests, linter). node_modules/ is ignored: dependencies are installed inside the image, for the right system.

Common mistakes:

  • Copying your own node_modules folder into the image: it may contain modules compiled for macOS or Windows, unusable on Linux.
  • Putting USER node before RUN npm ci without giving that user write access to /app: the installation fails. Here, we switch user at the end, just before start-up.
  • Writing CMD node server.js without brackets: the command then goes through a shell, which does not correctly pass the stop signal to Node.js.

Summary

  1. 1A Dockerfile describes an image: FROM, WORKDIR, COPY, RUN, EXPOSE, CMD.
  2. 2docker build -t name:version . builds the image from the context, the current folder.
  3. 3Each instruction is a cached layer; a changed layer rebuilds all the following ones.
  4. 4Copy and install the dependencies before the code to keep the cache.
  5. 5A .dockerignore excludes useless files and secrets; the application runs as a non-root user.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.