Docker ignores patterns in .dockerignore
# Ignore generated files *.pyc
docker build ignores this and copies the entire directory tree that looks like the following:
/contextdir/ |-- Dockerfile \-- src/ |-- a.py # this is copies - all right \-- a.pyc # this should be ignored, but is copied too. Why?
2 Solutions collect form web for “Docker ignores patterns in .dockerignore”
Finally, I understood what’s the 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?
The reference at https://docs.docker.com/engine/reference/builder/#/dockerignore-file doesn’t put clear enough that 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 docs for .dockerignore state that 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.