I intended to exclude python generated files from the docker image. So I added .dockerignore
at the root of the context directory:
# Ignore generated files *.pyc
Alas 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?
What if you ignore the dockerfile? It is true that you can also mention the dockerfile inside the . dockerignore file and exclude it from the Docker build context. In fact, it is a common practice than you might have thought.
The documentation says that yes it can. You can even use the . dockerignore file to exclude the Dockerfile and . dockerignore files.
gitignore file in that it allows you to specify a list of files or directories that Docker is to ignore during the build process.
dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With