Sub topic 9
Keeping files out with .dockerignore
This chapter assumes you can run docker build and docker run, and that you know what an image layer is. You should also have read the chapters on the build context and on COPY.
What .dockerignore does
When you run docker build ., Docker sends the contents of the build context directory to the builder. A .dockerignore file lists paths that are left out of that context. Excluded files are never sent, so COPY and ADD cannot see them.
This has three effects:
- The build starts faster, because less data is transferred. This matters most for large directories such as
node_modulesor.git. - The image stays smaller, because
COPY . .does not pick up files you never wanted inside it. - The cache stays valid more often, because a changed log file or editor swap file no longer changes the input of
COPY . ..
A fourth benefit is safety. Files such as .env or private keys can be excluded so that a broad COPY . . does not put them into an image layer.
A basic file
Place .dockerignore in the root of the build context, next to the Dockerfile. Each line is a pattern, relative to the context root.
.git
node_modules
*.logWith this file, the .git directory, the node_modules directory, and any .log file in the context root are excluded. Lines starting with # are comments.
Here is the Dockerfile from the cache chapter again:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]Without a .dockerignore, COPY . . copies everything in the context. If a log file changes between builds, the checksum of that layer changes and the cache for that layer and all later layers is lost. With the file above, the log file is not part of the context, so the layer is reused.
Pattern rules
Patterns use Go's filepath.Match rules, with a few additions.
| Pattern | Meaning |
|---|---|
| `*.log` | Matches `.log` files in the context root only |
| `**/*.log` | Matches `.log` files at any depth |
| `temp?` | Matches `temp` plus one character, such as `temp1` |
| `!keep.log` | Exception: includes a file that an earlier pattern excluded |
Two details cause most mistakes:
- A single
*does not cross directory boundaries.*.logwill not matchlogs/app.log. Use**/*.logfor that. - Order matters for
!exceptions. The last matching pattern decides. Put the exception after the pattern it overrides.
*.md
!README.mdThis excludes every Markdown file in the context root except README.md.
How it fits into the build
Common errors
- The file is in the wrong place. Docker reads
.dockerignorefrom the context root, not from the directory you run the command in. Withdocker build -f docker/Dockerfile ., the file belongs in.. - Ignoring a file the Dockerfile needs. If you exclude
requirements.txtand then writeCOPY requirements.txt ., the build fails because the file is not in the context. Docker also cannot copy the Dockerfile itself if you exclude it, though the builder still reads it. - Expecting it to affect
RUN..dockerignoreonly filters what is sent into the context. Commands inRUNthat download files are not affected.
Exercise
A project has COPY . . in its Dockerfile and a .dockerignore containing *.log. The build still copies logs/debug.log into the image. Why, and how do you fix it?
Show answer
A single * does not match across directories, so *.log only matches .log files in the context root. Change the pattern to **/*.log to exclude .log files at any depth.
Exercise
Why can adding .git to .dockerignore make rebuilds faster even if your Dockerfile never mentions .git?
Show answer
COPY . . includes .git unless it is excluded. Git rewrites files in that directory on commits and fetches, which changes the checksum of the copied layer and invalidates the cache. Excluding it also means less data is sent to the builder.