Fix Cloud not find a valid Docker environment TestContainers

If you see this issue:

java.lang.ExceptionInInitializerError
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: 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:256)
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:247)
	at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:150)
	at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:186)
	at org.testcontainers.DockerClientFactory$1.getDockerClient(DockerClientFactory.java:104)
	at com.github.dockerjava.api.DockerClientDelegate.authConfig(DockerClientDelegate.java:108)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:321)
	at io.ukata.api.core.system.AbstractIntegrationTest.<clinit>(AbstractIntegrationTest.java:24)
	... 2 more

That means testcontainers cannot find a docker env to run.

There are a few things you can do.

Check .testcontainers.config

Open .testcontainers.properties file under your home directory. If that file exists, make sure it points to the right docker host.

Enable docker sock In Docker Desktop

If you use Docker desktop, make sure this option is checked (Allow the default Docker socket to be used):

After that, you need to restart docker desktop.

Verify the fix

In my case, after enabling /var/run/docker.sock, I can run the tests:

Leave a Comment