Skip to main content
Dockerfile Image A Dockerfile is a “script for building an image”: one instruction per line, starting from a base image and step by step copying files, installing dependencies, and setting up the environment to produce a reproducible image. This page covers how it relates to the build context and builds one from scratch. It assumes you can already use the basic docker commands (see CLI commands).

Build context and tag

In docker build -t myapp:1.0 .:
  • -t myapp:1.0: the image tag (name:version, omitting the version defaults to latest).
  • The trailing .: the build context. The builder only sees files inside the context, not paths outside it.
  • -f: specifies the Dockerfile location (defaults to Dockerfile in the context root).
project dir (build context)
├── Dockerfile          ← list of instructions
├── .dockerignore       ← exclusion list
├── app.py
└── requirements.txt

.dockerignore

Same syntax as .gitignore, sits in the context root, and keeps things out of the builder, shrinking the context and avoiding needless cache invalidation:
.git
__pycache__
*.pyc
.env
.venv
node_modules
*.md
tmp/
Supports ** and ! (negate to keep). A bigger context ships slower, so .dockerignore is a file worth preparing by default.

Building a Dockerfile from scratch

1

Pick a base image (FROM)

Start from a suitable base, pinning a version tag rather than latest (see Choosing images):
FROM python:3.12-slim
2

Set the working directory (WORKDIR)

Following COPY / RUN are relative to this; the directory is auto-created if missing:
WORKDIR /app
3

Install dependencies, copy code (COPY + RUN)

Copy the manifest first, install, then copy the source (order matters, see layer cache):
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
4

Set the start command (CMD)

What runs when the container starts:
CMD ["python", "app.py"]
5

Build the image

Run it from the Dockerfile’s directory; . is the build context:
docker build -t myapp:1.0 .
A build (with BuildKit) looks like this, each layer matching an instruction:

Next

Reference: docs.docker.com/build/concepts/context