LESSON · 1 OF 7

What a Dockerfile is

What a Dockerfile is

So far you have run images other people built. Now you build your own. A Dockerfile is the recipe for an image - a plain text file listing the steps to assemble it, one instruction per line.

From recipe to image to container

Three things, in order:

  • The Dockerfile is the written recipe.
  • docker build reads the recipe and produces an image.
  • docker run starts a container from that image.

You write the Dockerfile once. Anyone can build the same image from it, and run identical containers anywhere. The Dockerfile is what makes an image reproducible - it is the source code of the image.

What it looks like

A Dockerfile is a list of instructions, each an uppercase keyword followed by its arguments:

dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
RUN pip install requests
CMD ["python", "app.py"]

Read top to bottom, this says: start from the Python slim image, work in /app, copy app.py in, install a library, and by default run the app. Each instruction adds to the image.

The file is named Dockerfile

By convention the file is named exactly Dockerfile, with no extension, in the root of your project. docker build looks for that name automatically. You can point at a differently named file when you need to, but the default is Dockerfile.

The instructions this topic covers

The next nodes take the core instructions one at a time:

  • FROM - the base image to build on
  • RUN - run a command while building the image
  • COPY - copy files from your project into the image
  • WORKDIR - set the working directory
  • CMD - the default command a container runs

These five are enough to build a real image, which you will do by the end of the topic.

Spin up a fresh environment and practice live.
docker · fresh machine · ready in under a minute