Could not find a valid Docker environment issue
已回答
Hi All,
I have installed and setup IntelliJ on a new laptop (Mac M1).
Currently the integration tests failed because can't run docker from the IDE.
I have my docker running properly from CLI.
These are error in the console:
ERROR DockerClientProviderStrategy Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
ERROR DockerClientProviderStrategy UnixSocketClientProviderStrategy: failed with exception RuntimeException (java.lang.UnsatisfiedLinkError: Can't load library: /Users/<user>/Library/Caches/JNA/temp/jna16301762796732536306.tmp). Root cause UnsatisfiedLinkError (Can't load library: /Users/<user>/Library/Caches/JNA/temp/jna16301762796732536306.tmp)
ERROR DockerClientProviderStrategy UnixSocketClientProviderStrategy: failed with exception RuntimeException (java.lang.NoClassDefFoundError: Could not initialize class org.testcontainers.shaded.com.github.dockerjava.okhttp.UnixSocketFactory$1). Root cause NoClassDefFoundError (Could not initialize class org.testcontainers.shaded.com.github.dockerjava.okhttp.UnixSocketFactory$1)
ERROR DockerClientProviderStrategy As no valid configuration was found, execution cannot continue
Anyone has seen this before or has any idea for to debug it?
I'm new in Java env, any suggestion would be very appreciated! thanks
请先登录再写评论。
Most probably, one of the containers does not have arm version. See similar problem: https://stackoverflow.com/questions/72716456/java-lang-illegalstateexception-could-not-find-a-valid-docker-environment-plea
Thanks for your answer, yes it was due a JNA library that incompatible with apple arm.
Able to fix it by upgrading JNA to 5.8.0
https://medium.com/@ismailhossainraju74/defeating-the-badrequestexception-in-testcontainers-with-docker-desktop-on-windows-c7cdfaaf7b68
The Solution: Two Paths to Victory
There are two ways to solve this problem: the quick local fix, and the permanent project-wide fix.
1. The Quick Fix: Override the API Version Locally
If you are stuck on an older version of Testcontainers and can’t easily upgrade your project’s dependencies, you can force the `docker-java` client to use a modern API version that Docker Desktop accepts.
You can do this by creating a properties file in your user’s home directory.
1. Navigate to your user home directory (e.g., `C:\Users\YourUser\`).
2. Create a new file named `.docker-java.properties`.
3. Add the following line to the file:
api.version=1.44
Note: Depending on your exact Docker Desktop version, you might need to try `1.43` or `1.45`, but `1.44` is currently a safe bet.
When you run your tests again, the `docker-java` client will read this file, use the newer API version, and successfully bypass the Docker Desktop proxy block.
2. The Permanent Fix: Upgrade Testcontainers
The most robust solution is simply to update the Testcontainers library in your project. The Testcontainers team is aware of these Docker Desktop changes, and newer versions default to a compatible API version.
Update your `pom.xml` or `build.gradle` to use Testcontainers version `1.21.4` or higher (or upgrade to the `2.x` series).
For Gradle(groovy):
testImplementation “org.testcontainers:testcontainers:1.21.4”
testImplementation “org.testcontainers:postgresql:1.21.4” // update related modules too
For Maven(xml):
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.21.4</version>
<scope>test</scope>
</dependency>
Ensure TCP is Exposed
While the API version mismatch is the root cause of the `400 Bad Request`, named pipe connectivity on Windows WSL2 can sometimes be flaky. As a best practice, ensuring your Docker daemon is exposed on TCP provides a reliable fallback for Testcontainers.
1. Open Docker Desktop.
2. Go to Settings > General.
3. Check the box for ”Expose daemon on tcp://localhost:2375 without TLS”.
Debugging Docker networking on Windows can sometimes feel like chasing ghosts. In this case, an error that looked like a bad named-pipe configuration was actually an API version enforcement mechanism by Docker Desktop.
By upgrading Testcontainers or explicitly setting the `api.version` in `.docker-java.properties`, you can appease the Docker Desktop proxy and get your integration tests back to green.
My Docker version was Docker version 29.4.0, build 9d7ad9f .
Jhipster version was 8.5.0.