testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.02k stars 1.65k forks source link

.dockerignore file is not honored by ImageFromDockerfile #1534

Open aguibert opened 5 years ago

aguibert commented 5 years ago

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]
  2. Run docker build . and verify it fails
  3. Run equivalent command through TC API and it passes (it should fail due to invalid syntax)
    new ImageFromDockerfile()
                        .withFileFromPath(".", Paths.get(".")))
aguibert commented 5 years ago

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

Pentadome commented 10 months ago

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

sargue commented 2 months ago

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.

kiview commented 2 months ago

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