1

I use TestContainers for my integration tests. Currently, I use withCopyFileToContainer(MountableFile.forClassPathResource(...)) to copy some files from the host to a test container when starting the test container (so the service in which the tests reside is packaged in a jar and run locally on the host and the jar itself contains some files which need to be copied to the test containers which are started from the tests). This does not work when the service itself which contains the tests is containerized (so the jar file containing the files is started in a container, has access to the host docker via the docker socket, and starts new test containers to which some files need to be copied). I assume Test Containers is not able to copy a file from a container to another container. What would be the solution?

  1. Should I implement the Transferable interface and provide an implementation for the transferTo method?
  2. Should I use some kind of volumes? Any ideas are welcome.
Nfff3
  • 321
  • 8
  • 24

2 Answers2

1

Maybe the most practical way to share files between host and containers (and between containers) is to use mount volumes as you indicate in option 2.

Here is an example of mounting a volume from host on container:

return new GenericContainer<>("castlemock/castlemock:v1.63").withExposedPorts(8080).withCreateContainerCmdModifier(
    cmd -> cmd.withHostConfig(
            new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(28080), new ExposedPort(8080))))
        .withBinds(
            Bind.parse(Paths.get(".", "container_shares/castlemock").toAbsolutePath() + ":/root/.castlemock")));

In this case it is mounting folder "./container_shares/castlemock" (where "." represents the base directory of the project) from host on container in path "/root/.castlemock".

You can share files between containers by mounting the same "host" directory.

pringi
  • 3,987
  • 5
  • 35
  • 45
1

For your use case, you might need to fall back to interacting with docker-java directly, using withCreateContainerCmdModifier to attach a named volume (not a bind mount), that can be shared between the containers.

Kevin Wittek
  • 1,369
  • 9
  • 26