Sub topic 3
Defining services
This chapter assumes you can read YAML, know what an image and a container are, and have seen the layout of a compose.yaml file from the previous chapter.
What a service is
A service is a named recipe for running one kind of container. Compose reads the recipe and creates one or more containers from it. The service is the definition. The container is a running instance of it.
Compose names containers after the project, the service, and an index, such as shop-web-1. Other services on the same network reach it by the service name web, not by the container name.
The keys you use most
Every service needs image, build, or both. The other keys change how the container runs.
| Key | Purpose |
|---|---|
| `image` | Image to pull, or the tag to give a built image |
| `build` | Path to a build context, or a build block |
| `command` | Replaces the image's `CMD` |
| `entrypoint` | Replaces the image's `ENTRYPOINT` |
| `environment` | Variables set in the container |
| `ports` | Ports published on the host |
| `working_dir` | Directory the process starts in |
| `restart` | What to do when the container exits |
A worked example
name: demo
services:
cache:
image: redis:7
command: ["redis-server", "--maxmemory", "64mb"]
restart: unless-stopped
web:
build: .
image: demo-web:dev
working_dir: /app
command: ["python", "-m", "http.server", "8000"]
environment:
CACHE_HOST: cache
ports:
- "8000:8000"The cache service pulls redis:7 and overrides its default command. The web service has both build and image. Compose builds from the local Dockerfile and tags the result demo-web:dev. The CACHE_HOST value is simply the service name cache, which works because Compose adds each service to a shared network with that DNS name.
Command and entrypoint
Write command and entrypoint as lists. A list runs the program directly. A plain string is split into words without a shell, so pipes, &&, and $VAR expansion do not work. When you need them, call a shell explicitly:
services:
job:
image: alpine:3.20
command: ["sh", "-c", "echo started && sleep 30"]Setting entrypoint in Compose also clears the image's default CMD. If the container then starts with the wrong arguments, set command as well.
Common errors
- Neither
imagenorbuild. Compose stops with a message that the service has neither an image nor a build context. Add one of them. - Unquoted port mappings. YAML can read
22:22as a number in base 60. Always quote them, as in"8000:8000". - Expecting
container_nameto help. Setting it fixes the container's name, but you can then run only one container for that service. Leave it out unless something external needs the name. - Using the container name as a hostname. Other services should connect to the service name.
- Wrong indentation. A key indented one level too deep becomes part of the previous key. Run
docker compose configto see how Compose parsed the file.
Exercise
A service named api sets entrypoint: ["python", "server.py"] and no command. The image's Dockerfile has CMD ["--debug"]. Does --debug reach the program? How do you pass arguments to it?
Show answer
No. Overriding entrypoint in Compose clears the image's default CMD, so the program runs as python server.py alone. To pass arguments, set command explicitly, for example command: ["--debug"].
Exercise
Why should you write ports: ["8000:8000"] with quotes, and why does command: "echo hi | cat" not behave like a shell line?
Show answer
Quotes keep YAML from reading values such as 22:22 as a base-60 number. A string command is split into words and run without a shell, so the pipe is passed as a literal argument. Use ["sh", "-c", "echo hi | cat"] instead.