testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

.dockerignore file is not honored by ImageFromDockerfile

Open aguibert opened this issue 6 years ago • 4 comments

For Docker build performance, it can be very important for certain projects to have a .dockerignore file so that extra content is not sent to the Docker daemon (which may be on a different machine).

It appears that the underlying docker-java API does support .dockerignore files: https://github.com/docker-java/docker-java/blob/master/src/test/java/com/github/dockerjava/cmd/BuildImageCmdIT.java#L176

However, if I try an invalid .dockerignore in one of my Testcontainers projects, then it does not appeared to be honored. I was able to confirm this with the following steps:

  1. Add invalid content in a .dockerignore file like this:
*~
[a-b-c]
  1. Run docker build . and verify it fails
  2. Run equivalent command through TC API and it passes (it should fail due to invalid syntax)
new ImageFromDockerfile()
                        .withFileFromPath(".", Paths.get(".")))

aguibert avatar Jun 06 '19 16:06 aguibert

The problem here seems to be that we always call BuildImageCmdImpl(BuildImageCmd.Exec exec, InputStream tarInputStream) which doesn't set a Dockerfile in the docker-java layer. Setting an explicit Dockerfile is what causes the corresponding .dockerignore to be read.

To resolve this issue, we can call withDockerfile(File) on the underlying docker-java API

aguibert avatar Jun 06 '19 20:06 aguibert

I have the same problem with dotnet testcontainers. Any workarounds?

Pentadome avatar Dec 22 '23 14:12 Pentadome

I'm also affected from this issue.

As this hasn't been resolved for several years now, does anyone know of a workaround?

Apart from the obvious one to not include everything and "manually" filtering out resources.

sargue avatar Aug 19 '24 09:08 sargue

I guess if someone wants to have a look at it, starting with the suggestion by @aguibert sounds promising: https://github.com/testcontainers/testcontainers-java/issues/1534#issuecomment-499664588

kiview avatar Aug 19 '24 09:08 kiview