Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker ignores patterns in .dockerignore

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? 
like image 245
Ilia Barahovsky Avatar asked Oct 26 '16 11:10

Ilia Barahovsky


People also ask

What should I ignore in Dockerignore?

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.

Should I put Dockerfile in Dockerignore?

The documentation says that yes it can. You can even use the . dockerignore file to exclude the Dockerfile and . dockerignore files.

Does Docker ignore files in Gitignore?

gitignore file in that it allows you to specify a list of files or directories that Docker is to ignore during the build process.

Where should Dockerignore file be?

dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.


2 Answers

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 
like image 118
Ilia Barahovsky Avatar answered Sep 25 '22 04:09

Ilia Barahovsky


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.

like image 31
Elton Stoneman Avatar answered Sep 23 '22 04:09

Elton Stoneman