Keep one file with an ordered Docker ignore exception
Admin ·
Check the file paths that matter to your Docker build context. An exception near the end of a .dockerignore file can keep a needed file after a wider rule excludes it.
Compare root files and nested paths
Select Load example and Check and generate. The rule *.md excludes matching Markdown names at the context root. The following !README*.md includes matching root README files. The nested docs/notes.md has no matching rule in this example.
The preview also shows src/node_modules/demo/index.js excluded. The **/node_modules pattern matches its parent directory even when that directory is nested.
| Path | Decision | Last matching rule |
|---|---|---|
| README.md | Included by exception | !README*.md |
| README-private.md | Excluded | README-private.md |
| docs/notes.md | Included | No rule matched |
| src/node_modules/demo/index.js | Excluded | **/node_modules |
Docker build context and .dockerignore filesMoby patternmatcher v0.6 parent path matching
Move an exception and check the result again
Move README-private.md above !README*.md and select Check and generate again. The final matching rule for README-private.md is now the exception. Its row changes to Included by exception.
Select Download .dockerignore ZIP, then extract and open .dockerignore as text. Confirm that README-private.md appears before !README*.md. If the private file should stay excluded, restore its rule below the exception.
Use the correct ignore file for the build context
Place the file in the directory supplied as the build context. You can load a project preset and edit the patterns before generating the file. For example, the Node.js preset includes .env* with a later exception for .env.example.
Docker supports an ignore file named for a specific Dockerfile. When that file and the root .dockerignore both apply, Docker gives the Dockerfile-specific ignore file precedence. Preview the rules for the build you plan to run.
Docker build context and .dockerignore filesMoby patternmatcher v0.6 ignore file normalization
Common questions
How should I write a comment?
Put # in the first column. The Docker ignore reader identifies comments before trimming other lines.
What happens if I start with a blank rule list?
The downloaded ZIP can contain an empty .dockerignore. With no active patterns, the path preview includes every supplied valid path.