Fix Cloud not find a valid Docker environment TestContainers

If you see this issue:

	at java.base/java.lang.reflect.Method.invoke(
	at java.base/java.util.ArrayList.forEach(
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(
	at java.base/java.util.Optional.orElseThrow(
	at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(
	at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(
	at org.testcontainers.DockerClientFactory.client(
	at org.testcontainers.DockerClientFactory$1.getDockerClient(
	at com.github.dockerjava.api.DockerClientDelegate.authConfig(
	at org.testcontainers.containers.GenericContainer.start(
	at io.ukata.api.core.system.AbstractIntegrationTest.<clinit>(
	... 2 more

That means testcontainers cannot find a docker env to run.

There are a few things you can do.

Check .testcontainers.config

Open 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