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?
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?
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.
Videos
If you need to ignore everything except some directories or files and also ignore some unnecessary files inside those allowed directories you can use the following .dockerignore file:
# Ignore everything
*
# Allow files and directories
!/file.txt
!/src
# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
From the dockerfile reference:
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.
So a line containing simply ** will ignore everything in the same directory as the Dockerfile.
As expected the exclamation can then be used to reference any files you do wish to send to the docker daemon.