Sub topic 5
The build context and .dockerignore
This chapter assumes you can write a basic Dockerfile and run docker build. You should also know that each instruction in a Dockerfile creates a layer.
What the build context is
When you run docker build -t myapp:1.0 ., the final . is the build context. It is the set of files the builder can see. The client sends these files to the builder before the first instruction runs.
A COPY or ADD instruction can only read files that are inside the context. A path such as COPY ../secrets.txt / fails, because .. points outside it.
The context is not the same as the Dockerfile location. By default the builder looks for a file named Dockerfile at the root of the context. You can choose another one with -f.
docker build -f docker/Dockerfile.prod -t myapp:1.0 .Why a large context hurts
Everything in the context is transferred, even files your Dockerfile never copies. A large context causes three problems:
- Slow builds, because the transfer happens on every build.
- Cache misses.
COPY . .includes every file in the context. If any of them changes, the cache for that layer and all later layers is invalidated. - Leaked data. Files such as
.envor.gitcan end up inside an image layer, and anyone who can pull the image can read them.
Excluding files with .dockerignore
Place a file named .dockerignore at the root of the context. Each line is a pattern for files the builder should leave out.
.git
node_modules
*.log
.envThis keeps version-control data, locally installed dependencies, logs, and secrets out of the build. Local node_modules is a good example. Your image should install its own dependencies for its own platform, and your host's copy may be built for a different one.
Common pattern rules:
| Pattern | Effect |
|---|---|
| `*.log` | Excludes matching files in the root of the context |
| `**/*.log` | Excludes matching files at any depth |
| `tmp` | Excludes the file or directory named `tmp` |
| `!keep.log` | Adds an exception to an earlier rule |
Rules are evaluated in order, and the last matching rule wins. Put an exception after the rule it overrides.
Checking what is sent
The builder prints how much context it transferred. With BuildKit, look for the "transferring context" line in the build output. If the size is far larger than your source code, something is missing from .dockerignore.
Common errors
COPY failed: file not found: the file is either outside the context or excluded by.dockerignore. Check both.- Ignoring the Dockerfile itself: the builder still needs it, so the Dockerfile is always sent, even if a pattern matches it.
- Wrong context directory: running
docker build .from a subdirectory changes whatCOPYcan see. Always check where you run the command.
Exercise
Your build runs COPY . ., and the layer is rebuilt on every build even though you only edited the README. Your project also contains a .git directory and a debug.log file that changes constantly. What should you do?
Show answer
Add .git and *.log to .dockerignore. These files are part of the context, so changes to them invalidate the COPY . . layer. Excluding them keeps them out of the context and out of the cache key. Editing the README still invalidates that layer, because it is copied. To avoid that, copy dependency files first and the rest of the source later.