Docker ignore target directories created by mave using .dockerignore file

2.8k views Asked by At

I am building maven project using maven docker image. Maven project is very big like more than 300mb source code. So when I run docker build it needs to send all build context[source code] to Docker daemon. The issue is suppose if I have already build my maven project then source code size becomes more than 5gb including all the target directories.

So in .dockerignore I need to ignore all the /target/ directories.

I tried this but its not working

.git
**/target/*.*
2

There are 2 answers

0
Muhammad Waqas Dilawar On

It should be same as we do it in .gitignore, so to ignore target folder just use target/.

0
Leo On

None of the answers above seemed to work for me (docker version 20.10.5). In the end, the following worked out OK:

.dockerignore:

**/target/

Here is an excerpt from the .dockerignore documentation:

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.