Reputation: 10498
How can I exclude python generated files like *.pyc
in all subdirectories from the docker image?
I added .dockerignore
at the root of the context directory:
# Ignore generated files
*.pyc
Alas docker build
ignores this at least for subdirectories and copies the entire directory tree that looks like the following:
/contextdir/
|-- Dockerfile
\-- src/
|-- a.py # this is copied - all right
\-- a.pyc # this should be ignored, but is copied too. Why?
Upvotes: 63
Views: 44219
Reputation: 10498
Patterns like *.pyc
are matched only at the beginning of the path, or for the files directly below the context directory, but not recursively. To make it work recursively the **/
syntax must be used:
# Ignore generated files
**/*.pyc
The reference at How to create a dockerignore file doesn't put that clear enough.
Finally, I understood that trick. For some reason, I wasn't able to find any mention of this and haven't found any example Dockerfile
with such construct, so documenting it here. Was it trivial for everybody else?
Upvotes: 134
Reputation: 19154
The docs for .dockerignore state that the .dockerignore file is interpreted as a list of patterns similar to the file globs of Unix shells. The pattern matching use's Go's filepath matching logic. The docs also cover the recursive pattern:
Beyond Go’s filepath.Match rules, Docker also supports a special wildcard string ** that matches any number of directories (including zero). For example, **/*.go will exclude all files that end with .go that are found in all directories, including the root of the build context.
Upvotes: 11