Sub topic 8
Exceptions with ! and why order matters
This chapter assumes you can write a basic .dockerignore with wildcards, as covered in the previous chapter. You should also know how to run docker build and docker run.
What a ! line does
A line that starts with ! is an exception. It brings back files that an earlier line excluded. Use it when a broad pattern removes slightly too much.
# Docs are not needed in the image, except the README
*.md
!README.md*.md removes every Markdown file in the context root. !README.md puts README.md back, so COPY can still use it.
The last matching rule wins
Docker checks every line in the file from top to bottom, and the last line that matches a path decides what happens to it. A matching ! line includes the path. A matching plain line excludes it. If no line matches, the path stays in the context.
This is why order matters. Consider this file:
*.md
!README*.md
README-secret.mdHere is how each root-level file is handled:
| File | Matching lines | Result |
|---|---|---|
| `CHANGELOG.md` | `*.md` | Excluded |
| `README.md` | `*.md`, `!README*.md` | Included |
| `README-secret.md` | `*.md`, `!README*.md`, `README-secret.md` | Excluded |
| `server.js` | none | Included |
The third rule re-excludes README-secret.md after the exception had brought it back.
Putting the lines in the wrong order
Swap the lines so the specific exclusion comes first:
*.md
README-secret.md
!README*.mdNow !README*.md is the last line that matches README-secret.md, so the file is included. Nothing warns you. The file simply ends up in the context, and a later COPY . . can put it in the image.
The safe habit is to write rules from general to specific. Put the broad exclusion first, then exceptions, then exclusions of particular files.
Patterns match from the context root
The pattern *.md has no ** in it, so it applies only to files in the context root. A file such as docs/guide.md is not matched. To exclude Markdown files at any depth, write **/*.md. Exceptions follow the same rules, so !**/README.md brings back README files at any depth.
Verifying the result
Don't guess whether your rules work. Build a debug image that copies the filtered context, then list the files it received:
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD ["find", "."]Save it as Dockerfile.debug, then run:
docker build -t context-debug -f Dockerfile.debug .
docker run --rm context-debugThe output lists every file that survived filtering. If README-secret.md appears, reorder your rules.
Exercise
A .dockerignore file contains the three lines *.md, !README*.md and README-secret.md, in that order. The context root holds CHANGELOG.md, README.md, README-secret.md and docs/guide.md. Which of these files are in the build context? What happens to README-secret.md if you move its line to the top of the file?
Show answer
README.md and docs/guide.md are included. CHANGELOG.md and README-secret.md are excluded. docs/guide.md stays because *.md only matches files in the context root. If README-secret.md is moved to the top, !README*.md becomes the last matching rule for it, so the file is included again.