A Dockerfile is a script that describes how to build a container image: which base image to start from, what commands to run inside it, what files to copy in, and how to configure the result. Each instruction produces a layer, and the set of layers plus metadata makes up the final image.
A simple example:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]
Key instructions include FROM (base image), RUN (execute a command during build), COPY / ADD (copy files into the image), WORKDIR (set working directory), ENV (set environment variables), EXPOSE (document ports), USER (change user), and CMD / ENTRYPOINT (what runs when the container starts).
Good Dockerfiles are short, deterministic, and cache-friendly. Put instructions that change rarely (installing system packages) near the top, and instructions that change often (copying source code) near the bottom, so layer caching helps you. Multi-stage builds (FROM ... AS build followed by FROM ... copying only artifacts) let you use a large build image and ship a small runtime image.
Related terms: Docker, Container Image, Container
Discussed in:
- Chapter 17: Containers and Virtualisation — Dockerfile: Building Images
Also defined in: Textbook of Linux