Docker ignores patterns in .dockerignore Docker ignores patterns in .dockerignore docker docker

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 How to create a 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.