Sub topic 2
FROM: starting from a base image
This chapter assumes you can run docker build and docker run, and that you know an image is a read-only template that a container starts from. It covers the FROM instruction. Tag choice and pinning get their own chapter later.
What FROM does
Every Dockerfile begins with FROM. It names the image that your build starts from, called the base image. Each instruction after it adds a layer on top of that base. The final image contains the base image's layers plus your own.
FROM python:3.12-slimThe reference has the form name:tag. Here python is the repository and 3.12-slim is the tag. If you leave out the tag, Docker uses latest. Docker pulls the image from a registry (Docker Hub by default) if it is not already stored locally.
What the base image gives you
The base image supplies the filesystem and defaults your build inherits:
- Operating system files, such as a minimal Debian or Alpine userland.
- Language runtimes and tools, such as Python and pip.
- Metadata set by the base image's own Dockerfile, such as
ENV,WORKDIR,USER,CMD, andENTRYPOINT.
The last point matters. If the base image defines a CMD and you do not define one, your image uses the base image's. Later chapters on CMD and ENTRYPOINT build on this.
Special base images
Two base images are not ordinary distributions.
scratchis an empty image with no files at all. It is useful for static binaries. The multi-stage chapter uses it.- A distribution image such as
debian,ubuntu, oralpinegives you a general-purpose userland.
Language images such as python and golang are built on top of a distribution and add the runtime.
Naming a stage and using ARG
FROM can take an AS name. This starts a named build stage, which later chapters use for multi-stage builds.
FROM golang:1.22 AS buildARG is the only instruction allowed before FROM. It lets you parameterize the base image reference.
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim
RUN python --versionBuild with the default, or override it:
docker build -t demo .
docker build --build-arg PYTHON_VERSION=3.11 -t demo-311 .An ARG declared before FROM is available only in FROM lines. To use it after FROM, declare it again inside the stage with ARG PYTHON_VERSION.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `pull access denied` or `repository does not exist` | Misspelled image name, or a private image without login | Check the name, then run `docker login` if needed |
| `manifest unknown` | The tag does not exist in that repository | Look up valid tags on the registry |
| `no matching manifest for linux/arm64` | The image has no build for your CPU architecture | Pick another tag, or pass `--platform` |
| `Dockerfile parse error: ... FROM` | An instruction other than `ARG` appears before `FROM` | Move `FROM` to the top |
Check your understanding
Exercise
A Dockerfile starts with ARG VERSION=3.12, then FROM python:${VERSION}-slim, then RUN echo $VERSION. The RUN line prints an empty string. Why, and how do you fix it?
Show answer
An ARG declared before FROM is available only to FROM lines. It goes out of scope once the stage begins. Fix it by adding ARG VERSION again after FROM, which makes the value available inside the stage.
Exercise
You write FROM python with no tag. Which tag does Docker use, and why can that cause problems later?
Show answer
Docker uses latest. That tag moves as new versions are published, so the same Dockerfile can build against a different Python version on a later day and break your build or change its behavior.