Sub topic 8
Building Images from a Dockerfile in Compose
Prerequisites
This chapter assumes you can write a basic Dockerfile and know the difference between an image and a container. It also assumes you have read the earlier chapters on defining services and running the stack lifecycle.
The build key
A service normally names a ready-made image. If you add build, Compose builds the image from a Dockerfile instead. The short form takes a path to the build context. The context is the directory sent to the builder, and COPY can only read files inside it.
services:
web:
build: .The long form gives you more control:
services:
web:
image: shop-web:1.0
build:
context: .
dockerfile: Dockerfile
target: runtime
args:
PYTHON_VERSION: "3.12"
ports:
- "8000:8000"contextis a path relative to the Compose file, or a Git URL.dockerfileis a path relative to the context. The default isDockerfile.targetstops a multi-stage build at the named stage.argssets build arguments declared withARGin the Dockerfile.imagecombined withbuildtells Compose which name and tag to give the result.
Without image, Compose names the image after the project and service, such as shop-web.
Here is a Dockerfile that matches the file above:
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM base AS runtime
COPY src ./src
CMD ["python", "src/main.py"]Copying requirements.txt before the source code keeps the dependency layer cached. Editing code then only rebuilds the last layers.
When Compose builds
docker compose up builds an image only if it does not exist yet. If you change the source later, up reuses the old image. Use these commands to force a rebuild:
docker compose build
docker compose build web
docker compose build --no-cache web
docker compose up -d --buildThe first command builds every service that has a build section. The second builds only web. The third ignores the layer cache. The fourth rebuilds and then recreates any container whose image changed.
Build arguments are not runtime variables
An ARG exists only while the image is being built. A container's environment values exist only when it runs. They are separate settings, so a value set in one is not visible in the other.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Old code still runs after an edit | `up` reused the existing image | Run `docker compose up -d --build` |
| `COPY failed: file not found` | The file is outside the context, or `.dockerignore` excludes it | Widen `context` or edit `.dockerignore` |
| `dockerfile` not found | Path was written relative to the Compose file | Write it relative to the context |
| Build is slow every time | `COPY . .` comes before the dependency install | Copy dependency files first |
| Wrong stage runs | `target` is missing | Set `target` to the stage you want |
Add a .dockerignore file next to the Dockerfile. List things like .git, node_modules, and local .env files. A smaller context builds faster, and secrets stay out of the image.
Exercise
You edit a file under src/, run docker compose up -d, and the container still runs the old code. Why does this happen, and which single command fixes it?
Show answer
up only builds an image when none exists, so it reused the old one. Run docker compose up -d --build to rebuild the image and recreate the container.
Exercise
A service's build.context is ./app and dockerfile is docker/Dockerfile.prod. Where must that Dockerfile be on disk, relative to the Compose file?
Show answer
At ./app/docker/Dockerfile.prod. The dockerfile path is resolved relative to the context, not the Compose file.