Sub topic 4
Choosing a base image with FROM
This chapter assumes you can run docker build and docker run, and that you know an image is a stack of read-only filesystem layers that a container runs on top of. Every Dockerfile starts from some other image. The FROM instruction picks it. That choice affects image size, security exposure, compatibility, and how reproducible your builds are.
What FROM does
FROM sets the base image for the build stage that follows. Every layer in that base becomes the lowest layers of your image. Each later instruction adds a layer on top.
FROM node:20-slimThe general form is:
FROM [--platform=<platform>] <image>[:<tag>|@<digest>] [AS <name>]A Dockerfile must have at least one FROM. Only ARG instructions may appear before it. Each FROM begins a new build stage, which matters for multi-stage builds, covered in a later chapter.
Two things are easy to miss:
- The base image's metadata is inherited. Its
ENV,WORKDIR,USER,ENTRYPOINT, andCMDvalues carry into your image unless you override them. If your container behaves oddly, inspect the base withdocker inspect. - A base image is not a runtime requirement. It is only the starting filesystem. The kernel always comes from the host.
Reading an image reference
An image reference has up to four parts: registry, repository, tag, and digest.
| Reference | Meaning |
|---|---|
| `node` | Docker Hub official image `docker.io/library/node`, tag `latest` |
| `node:20-slim` | Tag `20-slim` of the same repository |
| `ghcr.io/acme/tool:2.1` | Repository on the GitHub registry, tag `2.1` |
| `node@sha256:ab12...` | Exact image content, identified by digest |
| `node:20-slim@sha256:ab12...` | Tag for humans, digest for enforcement |
If you leave out the tag, Docker uses latest. That tag is only a name. It does not mean "newest", and the maintainer decides what it points to. Relying on it means two builds on different days can start from different images.
Tags are mutable, digests are not
A tag is a pointer that the publisher can move. node:20-slim today may point to a different image than it did last month, because the maintainers rebuild it for security patches and minor releases. A digest is a SHA-256 hash of the image manifest. It identifies one exact image forever.
You can find the digest of an image you have pulled:
docker pull node:20-slim
docker inspect --format '{{index .RepoDigests 0}}' node:20-slimThen pin it in the Dockerfile:
FROM node:20-slim@sha256:0000000000000000000000000000000000000000000000000000000000000000The all-zero digest above is a placeholder. Replace it with the value printed by the command. Docker rejects a reference whose digest does not match.
Choose a pinning level based on what you need:
| Strategy | Example | Reproducible | Gets security patches automatically |
|---|---|---|---|
| Floating major | `node:20-slim` | Low | Yes |
| Specific version | `node:20.11.1-slim` | Medium | Partly |
| Digest | `node:20.11.1-slim@sha256:...` | High | No |
Digest pinning gives exact builds but freezes you on that image. You then need a process, such as an automated dependency-update bot, to bump the digest regularly. Without one, a pinned image slowly accumulates known vulnerabilities.
Common families of base images
Most official images come in several variants. The names vary by project, but the pattern is consistent.
| Variant | Typical contents | Size | Use when |
|---|---|---|---|
| Full (`node:20`) | Debian plus compilers, git, and many libraries | Largest | You must build native extensions and want tools available |
| Slim (`node:20-slim`) | Debian with only what the runtime needs | Medium | General runtime images where you still want a shell and `apt` |
| Alpine (`node:20-alpine`) | Alpine Linux with musl libc and BusyBox | Small | Size matters and your software works with musl |
| Distroless | Runtime and certificates, no shell or package manager | Small | Production images for static or single-runtime apps |
| `scratch` | Completely empty | Minimal | Statically linked binaries only |
Alpine and the libc question
Alpine uses musl libc, while Debian and Ubuntu use glibc. Most pure-language code runs on both. Problems appear with prebuilt native binaries, such as Python wheels or Node addons, which are often compiled against glibc. On Alpine they may fail to install or be compiled from source, which makes the build slower and the image larger than expected. Subtle behavior differences in DNS resolution and locale handling also exist. If you see "not found" errors for a binary that clearly exists, a missing glibc loader is a likely cause.
A smaller base is not automatically a better choice. Measure the final image and test your application on it.
Distroless and scratch
Distroless images contain your language runtime and little else. There is no shell, so docker exec -it container sh fails and RUN instructions that need a shell cannot execute in that stage. This reduces attack surface, but debugging gets harder. Many distroless images offer a debug tag that adds a BusyBox shell for troubleshooting.
FROM scratch starts with nothing. There is no CA certificate bundle, no timezone data, no /etc/passwd, and no libc. A Go binary built with CGO_ENABLED=0 can run there, but it will fail on HTTPS calls unless you copy certificates in.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Choosing a base: decision criteria
Work through these questions in order.
- What does the application need at runtime? A compiled static binary needs almost nothing. An interpreted app needs its runtime and often system libraries.
- Does it need native dependencies? If yes, prefer a glibc-based image (Debian slim) unless you have tested Alpine.
- Who maintains the image? Prefer Docker Official Images, Verified Publisher images, or images from the vendor of the runtime. Unknown images on a public registry can contain anything.
- How will you debug it? If you need a shell in production, avoid scratch and plain distroless.
- How often will you rebuild? Frequent rebuilds make floating tags tolerable. Rare rebuilds favor pinned versions plus a scheduled update.
- What does your scanner report? Fewer packages usually means fewer reported vulnerabilities, but check the actual scan results rather than assuming.
A reasonable default for a new application is the -slim variant of an official image, pinned to a specific version. Move to distroless or scratch in a multi-stage build when you want a smaller attack surface.
flowchart TDPlatforms and architecture
An image reference can resolve to different images depending on CPU architecture. Official images are usually published as multi-platform manifests, so node:20-slim returns an amd64 image on an Intel machine and an arm64 image on Apple silicon.
A common problem: you build on an arm64 laptop and deploy to an amd64 server. The image runs locally but fails on the server with exec format error. Fix it by building for the target platform:
docker build --platform linux/amd64 -t myapp:1.0 .You can also set the platform in FROM, but this hard-codes it into the file and is usually discouraged:
FROM --platform=linux/amd64 node:20-slimAn exception is a build stage that only produces architecture-independent output, where you may pin the build platform with the $BUILDPLATFORM variable to run the stage natively and fast.
Parameterizing FROM with ARG
ARG is the only instruction allowed before FROM. It lets you pass the version at build time.
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-slim
WORKDIR /appdocker build --build-arg NODE_VERSION=22 -t myapp:node22 .There is a scoping rule. An ARG declared before FROM is available only in FROM lines. To use it inside a stage, redeclare it without a value after the FROM:
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-slim
ARG NODE_VERSION
RUN echo "Building with Node ${NODE_VERSION}"Without the second ARG, ${NODE_VERSION} expands to an empty string inside the stage.
Common errors
pull access deniedorrepository does not exist. The name is misspelled, the image is private and you have not rundocker login, or you used the wrong registry host.manifest unknown. The repository exists but the tag does not. Check the tag list on the registry.- Build works today, fails next month. A floating tag moved to a new base, such as a new Debian release that dropped a package. Pin a version or digest.
exec format errorat run time. The image architecture does not match the host./bin/sh: not foundorexecutable file not found. A distroless or scratch stage has no shell, or an Alpine binary needs glibc.- Unexpectedly large image. You started from a full variant when slim would do, or you installed build tools in the final stage.
Exercises
Exercise
A Dockerfile begins with ARG APP_VERSION=1.4 and then FROM myorg/base:${APP_VERSION}. A later RUN echo $APP_VERSION prints an empty line. Why, and how do you fix it?
Show answer
An ARG declared before FROM is only in scope for FROM lines, not inside the build stage. Add ARG APP_VERSION (without a value) after the FROM line. It then inherits the default or any --build-arg value.
Exercise
Your team builds a Python service with compiled wheels. You are choosing between python:3.12-alpine and python:3.12-slim. Which would you start with, and what would you check before switching to the other?
Show answer
Start with python:3.12-slim, because it uses glibc and most prebuilt wheels target it. Before moving to Alpine, build and test the application there, and compare final image size and build time. Wheels may not install on musl and may compile from source, which can erase the size benefit.