dockertestcontainersdocker-in-docker

Testcontainer not running inside docker


I have just migrated my code to Testcontainer (for testing). If I run the test locally with Docker enabled, the test runs fine.

Now, the issue comes when I try to run the same test in Docker [Docker-in-Docker], I get the following error

Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.IllegalStateException: Could not connect to Ryuk at 172.17.0.3:59197 [in thread "main"]
    at org.testcontainers.utility.RyukResourceReaper.maybeStart(RyukResourceReaper.java:132)
    at org.testcontainers.utility.RyukResourceReaper.init(RyukResourceReaper.java:42)
    at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:245)
    at org.testcontainers.DockerClientFactory$1.getDockerClient(DockerClientFactory.java:108)
    at com.github.dockerjava.api.DockerClientDelegate.authConfig(DockerClientDelegate.java:109)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:321)

I checked the ryuk container, it's testcontainers/ryuk v 0.11.0, looking a bit deeper, I see it's running on 172.17.0.3 with port 8080, I verified this by pinging from my service terminal nc -zv 172.17.0.3 8080 and it returned open.

While running Docker, I'm passing e TESTCONTAINERS_HOST_OVERRIDE=172.17.0.3 as my environment variable and -v /var/run/docker.sock:/var/run/docker.sock as volume.

I'm running this in Docker Compose.

This is my dependency

 <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>postgresql</artifactId>
            <version>1.21.0</version>
            <scope>test</scope>
        </dependency>

The question here is, how do I resolve this error? What is the solution to this issue?


Solution

  • So, going from here, I have to update my environment variable from

    e TESTCONTAINERS_HOST_OVERRIDE=172.17.0.3
    

    to

    e TESTCONTAINERS_HOST_OVERRIDE=host.docker.internal
    

    and it worked