Services: image & build
A service is one component of your application: "the API", "the database", "the worker". In the file it is a
key under services:; at run time it becomes one or more containers that all share the same configuration. Every
service needs to know where its image comes from, and Compose offers two answers: image (use an existing image
from a registry) and build (build one from a Dockerfile). This lesson covers both, how they combine, and the keys
that control pulling and naming.
image: run something that already exists
image takes a normal image reference, the same string you would give docker run:
| Form | Example | Meaning |
|---|---|---|
| name | redis |
Docker Hub library/redis:latest (avoid: latest moves) |
| name:tag | postgres:18 |
a version tag |
| registry/path:tag | ghcr.io/acme/api:1.4.2 |
another registry |
| name@digest | nginx@sha256:… |
one exact, immutable image |
| tag + digest | nginx:1.29@sha256:… |
readable and pinned; the digest wins |
Compose does not rewrite or check the reference in config; a typo only shows up when the image is pulled. Image
names must be lowercase, and pinning a tag (or a digest) is what makes "it works on my machine" reproducible.
build: make the image from source
build points at a build context, a folder sent to BuildKit, with a Dockerfile in it. The short form is just
the path; the long form is covered in Building Images. If a service has build but no
image, Compose names the result <project>-<service>.
Both at once: build and tag
When a service has both keys, image becomes the name of the built image. This is the usual pattern for
your own code: developers build locally, CI builds and pushes, servers pull. Another service can reuse the same image
with a different command:
# $ docker compose config --no-path-resolution
# $ docker compose config --images
services:
api:
image: shop/api:${TAG:-dev}
build: ./api
worker:
image: shop/api:${TAG:-dev}
command: python -m worker$ docker compose config --no-path-resolution
name: demo
services:
api:
image: shop/api:dev
networks:
default: null
build:
context: ./api
dockerfile: Dockerfile
worker:
command:
- python
- -m
- worker
image: shop/api:dev
networks:
default: null
networks:
default:
name: demo_default
$ docker compose config --images
shop/api:dev
shop/api:dev
Only api builds; worker just uses the result. Notice --images lists the same image once per service.
With TAG=1.5 docker compose build && docker compose push, the image shop/api:1.5 goes to the registry, and a
server with the same file and TAG=1.5 pulls it instead of building.
Neither: an error
A service must have at least one of the two:
services:
api:
environment:
LOG_LEVEL: debugservice "api" has neither an image nor a build context specified: invalid compose project
pull_policy: when to pull
pull_policy decides what up does about the image:
| Value | Behaviour |
|---|---|
missing (default; alias if_not_present) |
pull only if the image isn't on the host |
always |
pull every time; fails if the registry is unreachable |
never |
never pull; fail if the image is missing |
build |
always build, even if an image with that name exists |
daily, weekly, every_12h |
pull if the last pull is older than that |
refresh |
check the registry and pull if the tag points somewhere new |
Values are validated against a pattern, so a made-up policy fails config:
services:
api:
image: shop/api:1.4
pull_policy: sometimesvalidating /home/ada/demo/compose.yaml: services.api.pull_policy 'sometimes' does not match pattern '^(always|never|build|if_not_present|missing|refresh|daily|weekly|every_([0-9]+[wdhms])+)