Sub topic 13
Context Layouts for Monorepos
This chapter assumes you know how docker build selects a context, how COPY resolves paths against it, and how .dockerignore filters files. It covers how to arrange the context when one repository holds several services.
The problem: one repository, several images
Suppose a repository contains two services and a shared library:
repo/
services/
api/
Dockerfile
src/
worker/
Dockerfile
src/
shared/
config/
.dockerignoreBoth services need files from shared/. If you build from inside services/api, the context is that directory, and COPY ../../shared . fails. A COPY source cannot point outside the context.
Option 1: one context per service
Each service is built with its own directory as the context.
docker build -t api ./services/apiThis gives a small context and good cache behavior, because changes in other services do not touch it. The cost is that the service cannot see shared code. Use this layout when services are independent.
Option 2: repository root as the context
Make the repository root the context and point -f at each Dockerfile.
docker build -t api -f services/api/Dockerfile .
docker build -t worker -f services/worker/Dockerfile .Inside the Dockerfile, every COPY path is relative to the repository root, not to the Dockerfile's directory.
FROM node:20-slim
WORKDIR /app
# Paths are relative to the repository root (the context)
COPY shared/config ./shared/config
COPY services/api/package.json services/api/package-lock.json ./services/api/
WORKDIR /app/services/api
RUN npm ci
COPY services/api/src ./src
CMD ["node", "src/server.js"]The tradeoff is that the builder receives the whole repository on every build. Without filtering, that is slow, and a change to an unrelated service can invalidate COPY . . layers. Copy specific paths instead of COPY . ., and filter with .dockerignore.
Filtering a shared context
When several Dockerfiles share one context, a single root .dockerignore applies to all of them. A rule that is right for one service may remove files another service needs. With BuildKit, you can place a Dockerfile.dockerignore file next to each Dockerfile to override the root file for that build.
repo/
services/api/Dockerfile
services/api/Dockerfile.dockerignore
services/worker/Dockerfile
services/worker/Dockerfile.dockerignore
.dockerignoreFor example, services/api/Dockerfile.dockerignore can exclude the worker's source:
.git
**/node_modules
services/workerNote that the file is named after the Dockerfile plus .dockerignore, so Dockerfile.dockerignore here matches a file called Dockerfile. A file named Dockerfile.prod would use Dockerfile.prod.dockerignore.
Comparing the layouts
| Layout | Context size | Shared code | Cache isolation |
|---|---|---|---|
| Context per service | Small | Not available | Strong |
| Root context, no filtering | Large | Available | Weak |
| Root context with per-Dockerfile ignore | Moderate | Available | Good |
Exercise
Exercise
Your repository has services/api/Dockerfile that must COPY shared/config. You run docker build -t api ./services/api and the build fails at that COPY. Give a corrected command, and say where the COPY source path is resolved from.
Show answer
Use the repository root as the context: docker build -t api -f services/api/Dockerfile .. COPY paths are then resolved from the repository root, so the Dockerfile uses COPY shared/config ./shared/config.