Skip to content

Can't connect to Docker running in WSL2 VM if Testcontainers runs on Windows and Docker Desktop is not used #4958

@saurabh4studies

Description

@saurabh4studies

Test container failed to start and throws Exception when try to run test case.

Host system: Windows 10
WSL2 based system: Ubuntu App for windows
Docker installed inside: Ubuntu App for windows

Using WSL2 based Ubuntu App on windows and setup docker in it. We are able to connect containers run inside it from host system i.e. Windows 10 through Ubuntu App IP address and port on which containers are running. In case of test containers it fails and throws below exception

Exception:

java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

	at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$7(DockerClientProviderStrategy.java:215)
	at java.util.Optional.orElseThrow(Optional.java:290)
	at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:207)
	at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:136)
	at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:178)
	at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14)
	at org.testcontainers.LazyDockerClient.authConfig(LazyDockerClient.java:12)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:310)
	at infrastructure.DatabaseContainer.start(DatabaseContainer.java:30)
	at com.ecw.hplus.chat.ChannelFeatureShould.before(ChannelFeatureShould.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Test case file:

import infrastructure.DatabaseContainer;
import infrastructure.MySqlDatabaseContainer;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class AFeatureShould extends SimpleRepositoryTest {

    private static final MySqlDatabaseContainer mysql = new MySqlDatabaseContainer("mysql.sql");
    
    @Parameterized.Parameters
    public static DatabaseContainer[] getDatabaseContainer() {
        return new DatabaseContainer[]{
                mysql
        };
    }

    @BeforeClass
    public static void before() {
        mysql.start();
    }

    @Test
    public void test() {
        // assertions
    }
}

Tried to mitigate this issue by modifying /etc/docker/deamon.json and C:/Users/myuser/.testcontainers.properties files and not succeeded.

/etc/docker/deamon.json:

{
        "hosts": ["unix:///var/run/docker.sock", "tcp://127.0.0.1:2375"]
}

C:/Users/myuser/.testcontainers.properties:

docker.client.strategy=org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy
docker.host=tcp\://<ip-of-ubuntu-app>\:2375

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions