Skip to content
DevOpsbeginner

What is a Dockerfile?

Updated Nov 30, 20263 min readBased on official documentation
What is a Dockerfile?

In short

A Dockerfile is a plain text file that lists the steps Docker follows to build an image. Each line is an instruction, such as starting from a base image, copying files, or setting a default command. Docker reads the file from top to bottom and runs the steps in order.

What a Dockerfile does

Docker packages software into images. An image is a ready-to-run bundle that holds an application and everything it needs to run. A Dockerfile is the recipe for building that image. You write it as a plain text file, and Docker reads it to assemble the image automatically.

The Dockerfile contains the same commands you could type by hand on the command line. Writing them down in one file means anyone can rebuild the same image later, on any machine that has Docker.

How it works

Each line in a Dockerfile is an instruction. Instructions are not case-sensitive, but the convention is to write them in UPPERCASE so they stand out from the arguments that follow them. Docker runs the instructions in order, from top to bottom.

A Dockerfile must begin with a FROM instruction. FROM names the base image you build on. Other common instructions include:

  • COPY, which copies files and folders into the image.
  • RUN, which executes a command while the image is being built.
  • ENV, which sets environment variables, named values the application can read.
  • WORKDIR, which changes the working directory for the following steps.
  • CMD and ENTRYPOINT, which set the default command the container runs when it starts.
  • EXPOSE, which describes the ports your application listens on.
  • USER, which sets the user and group ID the commands run as.

The official reference lists more instructions, including ADD, ARG, HEALTHCHECK, LABEL, VOLUME and others. You do not need all of them to begin.

A simple example

Here is a small Dockerfile for a hypothetical Python script:

FROM python:3.12-slim
WORKDIR /app
COPY script.py .
CMD ["python", "script.py"]

Line by line, this file starts from a base image that already has Python installed, sets the working folder to /app, copies the script into that folder, and sets the default command to run the script. You build it with a command such as docker build, and then start a container from the result.

Comments and formatting

Lines that begin with # are comments. Docker removes them before running the instructions, so you can use them to explain your steps. Leading whitespace before instructions is ignored, but whitespace inside an instruction's arguments is kept, so be careful with spacing in commands and paths.

Some optional lines at the very top of the file are called parser directives. They change how Docker reads the file. The syntax directive, for example, selects the Dockerfile syntax version. Parser directives must appear at the top, before any instruction, and each one can be used only once.

Common mistakes

The most common mistake is forgetting that a Dockerfile must start with FROM. Comments and parser directives may come first, and ARG lines may come before FROM, but no other instruction can.

Another mistake is putting large or unneeded files into the image. Copy only what the application needs. Keep the instructions in a sensible order too, because Docker can reuse earlier build steps that have not changed, which speeds up later builds.

Finally, remember that variables written as $name or ${name} are replaced when the Dockerfile is read. If you want a literal dollar sign, escape it with a backslash.

When to use it

Use a Dockerfile whenever you need a repeatable way to build an image, for example for a web service that must run the same way on a laptop and on a server. It also fits well into automated pipelines, where the build runs without manual steps.

If you are new to containers, read about the basic container ideas first, then return to the Dockerfile to see how those ideas are written down.

FAQ

Do I need to know Docker before writing a Dockerfile?

Basic familiarity helps. Knowing that an image is a packaged application and a container is a running copy of that image makes the instructions easier to follow.

Does the instruction name have to be uppercase?

No, Docker accepts any case. Uppercase is the convention because it makes instructions easy to tell apart from their arguments.

Why does my Dockerfile fail with an error about FROM?

Every Dockerfile must start with a FROM instruction that names a base image. Only comments, parser directives and ARG lines may appear before it.

Can I use comments in a Dockerfile?

Yes. Lines beginning with # are comments and are removed before the instructions run. Comments do not support line continuation characters.

Sources

This page is based on the official documentation below. Read it for full detail.

Dockerfile — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.