Dockerfile-maven: How does dockerfile maven plugin respect .dockerignore?

Created on 22 Jun 2017  路  7Comments  路  Source: spotify/dockerfile-maven

I have a .dockerignore file that reads like this:

!target/lib
!target/*.jar

docker build ... respected it and was able to copy over everything in target/lib and target/*.jar, but ignore rest of the targets.

When using mvn dockerfile:build, it actually returned an error:

INFO] Digest: sha256:0eca986ed1177c8ec71d41248a10a21f23a4650a9e7237699da0fa75d67e126c
[INFO] Status: Image is up to date for openjdk:8
[INFO]  ---> 7df8b534906e
[INFO] Step 2/5 : COPY target/lib /app/lib
[ERROR] lstat target/lib: no such file or directory
stale

Most helpful comment

TLDR: dockerfile-maven-plugin does not seem to do proper matching of directories. If you want to exclude/include directories, list them explicitly without wildcards. If you want to include/exclude subdirectories of excluded/included directories, use a wildcard under them, although this will not recurse. The ** arbitrary-depth match may not work at all with this plugin.

_The comments below apply to dockerfile-maven-plugin version 1.4.0._

For the original example, you would need to change the contents of your .dockerignore to

!target/lib/*
!target/*.jar

Although that will not include the contents of any subdirectories of target/lib, just its immediate contents. Using !target/lib/**/* does not help.


For example, given a build directory like the following

$ ls -al
total 72
drwxr-xr-x  13 dd  staff   416 Apr  4 10:52 .
drwxr-xr-x   3 dd  staff    96 Apr  4 08:11 ..
-rw-r--r--   1 dd  staff   167 Apr  4 10:52 .dockerignore
drwxr-xr-x  16 dd  staff   512 Apr  4 10:30 .git
-rw-r--r--   1 dd  staff   261 Apr  4 08:11 .gitignore
drwxr-xr-x   4 dd  staff   128 Apr  4 10:24 .mvn
-rw-r--r--   1 dd  staff   356 Apr  4 10:49 Dockerfile
-rw-r--r--   1 dd  staff   932 Apr  4 08:11 README.md
-rwxr-xr-x   1 dd  staff  6468 Apr  4 08:11 mvnw
-rw-r--r--   1 dd  staff  4994 Apr  4 08:11 mvnw.cmd
-rw-r--r--   1 dd  staff  2869 Apr  4 10:30 pom.xml
drwxr-xr-x   4 dd  staff   128 Apr  4 08:11 src
drwxr-xr-x  13 dd  staff   416 Apr  4 10:24 target

and a .dockerignore with these contents

*
!.dockerignore
!Dockerfile
!target/*.jar

using docker build . on the command line sends only the explicitly included files, .dockerignore, Dockerfile, and all of the *.jar files in target. Using dockerfile-maven-plugin, everything under the build directory gets sent to the docker daemon for the build _except_ the files in the root build directory that are not explicitly included, i.e., README.md, mvnw, mvnw.cmd, and pom.xml. The entire contents of the .git, .mvn, src, and target directories are sent. Adding the more permissive **/* has no effect, i.e., the contents of the .git, .mvn, src, and target directories are still sent.

Note also that unlike with the docker command line tool which will always send the Dockerfile and .dockerignore files, you must explicitly include them if they would otherwise be excluded. If you do not, the build will fail because there will be no Dockerfile.

If you want the contents of directories to not be sent to the docker daemon, list them explicitly, without any wildcard characters, in the .dockerignore. For the above example, a .dockerignore that works with both the docker CLI and dockerfile-maven-plugin has the following contents:

*
# dockerfile-maven-plugin does not handle `*` matching directories
# properly, so list directories
.git/
.mvn/
src/
target/
!.dockerignore
!Dockerfile
!target/*.jar

All 7 comments

.dockerignore works for me ! in my setup I use version 1.3.7 and I have "force upgraded" docker-client in the of the plugin like this :

```

com.spotify
docker-client
shaded
8.10.0

I had a similar issue. Initially I only had

**/target/*-config.jar

This worked when doing docker build, but not from maven. Adding the following made mvn dockerfile:build work as well

target/*-config.jar

TLDR: dockerfile-maven-plugin does not seem to do proper matching of directories. If you want to exclude/include directories, list them explicitly without wildcards. If you want to include/exclude subdirectories of excluded/included directories, use a wildcard under them, although this will not recurse. The ** arbitrary-depth match may not work at all with this plugin.

_The comments below apply to dockerfile-maven-plugin version 1.4.0._

For the original example, you would need to change the contents of your .dockerignore to

!target/lib/*
!target/*.jar

Although that will not include the contents of any subdirectories of target/lib, just its immediate contents. Using !target/lib/**/* does not help.


For example, given a build directory like the following

$ ls -al
total 72
drwxr-xr-x  13 dd  staff   416 Apr  4 10:52 .
drwxr-xr-x   3 dd  staff    96 Apr  4 08:11 ..
-rw-r--r--   1 dd  staff   167 Apr  4 10:52 .dockerignore
drwxr-xr-x  16 dd  staff   512 Apr  4 10:30 .git
-rw-r--r--   1 dd  staff   261 Apr  4 08:11 .gitignore
drwxr-xr-x   4 dd  staff   128 Apr  4 10:24 .mvn
-rw-r--r--   1 dd  staff   356 Apr  4 10:49 Dockerfile
-rw-r--r--   1 dd  staff   932 Apr  4 08:11 README.md
-rwxr-xr-x   1 dd  staff  6468 Apr  4 08:11 mvnw
-rw-r--r--   1 dd  staff  4994 Apr  4 08:11 mvnw.cmd
-rw-r--r--   1 dd  staff  2869 Apr  4 10:30 pom.xml
drwxr-xr-x   4 dd  staff   128 Apr  4 08:11 src
drwxr-xr-x  13 dd  staff   416 Apr  4 10:24 target

and a .dockerignore with these contents

*
!.dockerignore
!Dockerfile
!target/*.jar

using docker build . on the command line sends only the explicitly included files, .dockerignore, Dockerfile, and all of the *.jar files in target. Using dockerfile-maven-plugin, everything under the build directory gets sent to the docker daemon for the build _except_ the files in the root build directory that are not explicitly included, i.e., README.md, mvnw, mvnw.cmd, and pom.xml. The entire contents of the .git, .mvn, src, and target directories are sent. Adding the more permissive **/* has no effect, i.e., the contents of the .git, .mvn, src, and target directories are still sent.

Note also that unlike with the docker command line tool which will always send the Dockerfile and .dockerignore files, you must explicitly include them if they would otherwise be excluded. If you do not, the build will fail because there will be no Dockerfile.

If you want the contents of directories to not be sent to the docker daemon, list them explicitly, without any wildcard characters, in the .dockerignore. For the above example, a .dockerignore that works with both the docker CLI and dockerfile-maven-plugin has the following contents:

*
# dockerfile-maven-plugin does not handle `*` matching directories
# properly, so list directories
.git/
.mvn/
src/
target/
!.dockerignore
!Dockerfile
!target/*.jar

Thanks @ddgenome .
But it seems to me that even if I explicitly exclude all the directories. It does not help..

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Are there any plans on fixing this?

I am encountering the same problem.

Was this page helpful?
0 / 5 - 0 ratings