Sub topic 12
Per-Dockerfile Ignore Files
This chapter assumes you know what a build context is, how docker build PATH and -f select the context and the Dockerfile, and how patterns in a root .dockerignore work. It covers how to give different Dockerfiles in one context different filters.
The problem with one shared .dockerignore
A root .dockerignore applies to every build that uses that context. That becomes a problem when one context holds several Dockerfiles with different needs. A production Dockerfile may need to exclude tests, while a test Dockerfile needs them. Any single list is wrong for one of the two.
How Dockerfile-specific ignore files work
With BuildKit, Docker looks for an ignore file named after the Dockerfile, with .dockerignore appended. It looks for this file next to the Dockerfile. If the file exists, Docker uses it instead of the root .dockerignore for that build.
| Dockerfile passed with `-f` | Ignore file checked first | Fallback |
|---|---|---|
| `Dockerfile` | `Dockerfile.dockerignore` | `.dockerignore` |
| `docker/Dockerfile.prod` | `docker/Dockerfile.prod.dockerignore` | `.dockerignore` |
| `services/api/Dockerfile` | `services/api/Dockerfile.dockerignore` | `.dockerignore` |
The two files are not merged. If a Dockerfile-specific file exists, the root .dockerignore is ignored for that build. Copy over any rules you still want.
Worked example
Suppose a repository has one context and two Dockerfiles:
.
├── .dockerignore
├── Dockerfile
├── Dockerfile.dockerignore
├── Dockerfile.test
├── Dockerfile.test.dockerignore
├── src/
└── tests/The production image should leave out tests. Put this in Dockerfile.dockerignore:
.git
node_modules
.env
.env.*
testsThe test image needs tests/, so Dockerfile.test.dockerignore leaves it out of the list:
.git
node_modules
.env
.env.*Both builds use the same context. Only the filter changes:
docker build -t myapp .
docker build -t myapp-test -f Dockerfile.test .The first command reads Dockerfile.dockerignore. The second reads Dockerfile.test.dockerignore.
Use with a shared context
This feature pairs well with a repository-wide context. The Dockerfile sits in a subdirectory, but the context is the repository root so it can COPY from shared directories:
# Repository layout:
# services/api/Dockerfile
# services/api/Dockerfile.dockerignore
# shared/
# .dockerignore
docker build -t api -f services/api/Dockerfile .Here services/api/Dockerfile.dockerignore is used. Patterns in it are still relative to the context root, not to the directory that holds the ignore file. A rule meant to exclude services/web must be written as services/web, not ../web.
Common errors
- Using an old builder. The classic builder does not read Dockerfile-specific ignore files. Check that BuildKit is in use. It is the default in current Docker releases.
- Expecting a merge. Once
Dockerfile.dockerignoreexists, the root.dockerignorestops applying. Secrets listed only in the root file can then reach the build. - Wrong location or name. The file must sit next to the Dockerfile and match its full name. For
Dockerfile.prod, the name isDockerfile.prod.dockerignore, notDockerfile.dockerignore. - Paths relative to the wrong directory. Patterns are relative to the context root.
To confirm which files a build receives, use the debug image approach from the inspection chapter with -f pointing at a debug Dockerfile that has its own .dockerignore file.
Exercise
A repository has a root .dockerignore that excludes .env, and a file Dockerfile.dockerignore that excludes only tests. You run docker build -t app . with BuildKit. Is .env sent to the builder? Why?
Show answer
Yes. Because Dockerfile.dockerignore exists next to the Dockerfile, it replaces the root .dockerignore instead of adding to it. Its only rule excludes tests, so .env is part of the context. Add .env to Dockerfile.dockerignore to exclude it.
Exercise
You build with docker build -f docker/Dockerfile.prod .. What is the exact path of the Dockerfile-specific ignore file that BuildKit looks for, and what are its patterns relative to?
Show answer
It looks for docker/Dockerfile.prod.dockerignore. Its patterns are relative to the context root (the . argument), not to the docker/ directory.