testcontainers-java: Unable to run test containers with Apple Silicon Mac

% docker version                                                                                      
Client: Docker Engine - Community
 Cloud integration: 1.0.8
 Version:           20.10.3
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        48d30b5
 Built:             Fri Jan 29 14:28:27 2021
 OS/Arch:           darwin/amd64 (rosetta)
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.3
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       46229ca
  Built:            Fri Jan 29 14:32:07 2021
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Running docker via Docker Desktop Preview Version 3.1.0(60984) Disabled: Use gRPC FUSE for file sharing

Error:

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:215)
	at java.base/java.util.Optional.orElseThrow(Optional.java:408)
	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 example.mycompany.MySql.init(MySql.groovy:16)
	at example.mycompany.MySql.<clinit>(MySql.groovy:10)
	... 5 more

Using test containers in a Gradle build

    testImplementation(platform("org.testcontainers:testcontainers-bom:1.15.2"))
    testImplementation("org.testcontainers:mysql")
    testImplementation "org.testcontainers:selenium"

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 14
  • Comments: 39 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I can confirm that adding the following Maven dependency fixed this issue for me:

 <dependency>
               <groupId>net.java.dev.jna</groupId>
                <artifactId>jna</artifactId>
               <version>5.7.0</version>
                <scope>test</scope>
</dependency>

@sdelamo it is, thanks 😃

Your problem is related to JNA:

/Users/sdelamo/Library/Caches/JNA/temp/jna14927840726792078170.tmp: no matching architecture in universal wrapper

Consider either using JNA 5.7.0 that is compatible with M1 or use x86 build of JDK.

I have same issue with M1 Pro. And this will be solved with upgrade test container to newest version and add dependency jna

        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>mysql</artifactId>
            <version>1.16.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>

For those working with Gradle, adding the following solved the issue for me:

testImplementation 'net.java.dev.jna:jna-platform:5.8.0'

This thread saved me šŸ™ , in our case I needed to exclude JNA jars from very old elasticsearch version, e.g.:

<dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.elasticsearch</groupId>
          <artifactId>jna</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>

Ended up adding maven enforcer rule like:

          <plugin>
            <artifactId>maven-enforcer-plugin</artifactId>
            <configuration>
              <rules>
                <bannedDependencies>
                  <excludes>
                    <exclude>org.elasticsearch:jna</exclude>
                  </excludes>
                  <message>org.elasticsearch:jna is incompatible with arm64 / Apple M1, use net.java.dev.jna:jna instead</message>
                </bannedDependencies>
              </rules>
            </configuration>
          </plugin>

For me even after explicitly mentioning the dependency testImplementation 'net.java.dev.jna:jna-platform:5.8.0' , it did not work for me as one of the transitive dependency was using jna version 4.5.1. (transitive dependencies are dependencies that your component needs, but only because another dependency needs them)

So I had to force gradle to pick the newer version of jna which worked for me.

testImplementation('net.java.dev.jna:jna-platform'){
        version {
            strictly("[5.7,5.10]")
        }
    } 

Here is the reference to how to declare versions like I did above: https://docs.gradle.org/current/userguide/rich_versions.html#sec:strict-version

One can select what works for them, but since any version above 5.7 is compatible I have used this. (edited)

@sdelamo mysql does not provide arm images. For example https://hub.docker.com/_/mariadb does provide them, so you might try that.

I’m on testcontainers version 1.16.0 and it still does not work on my M1 Mac.

āžœ  ~ java -version
openjdk version "11.0.10" 2021-01-19 LTS
OpenJDK Runtime Environment (build 11.0.10+9-LTS)
OpenJDK 64-Bit Server VM (build 11.0.10+9-LTS, mixed mode)

Here are the logs:

2021-09-20 10:50:13.073  INFO [api,,] 52788 --- [           main] .t.d.DockerMachineClientProviderStrategy : docker-machine executable was not found on PATH ([/opt/homebrew/opt/helm@2/bin, /Users/nate/.asdf/shims, /opt/homebrew/opt/asdf/bin, /opt/homebrew/bin, /opt/homebrew/sbin, /usr/local/bin, /usr/bin, /bin, /usr/sbin, /sbin])
2021-09-20 10:50:14.122 ERROR [api,,] 52788 --- [           main] o.t.d.DockerClientProviderStrategy       : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2021-09-20 10:50:14.122 ERROR [api,,] 52788 --- [           main] o.t.d.DockerClientProviderStrategy       :     UnixSocketClientProviderStrategy: failed with exception RuntimeException (java.lang.UnsatisfiedLinkError: /Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp: dlopen(/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp, 1): no suitable image found.  Did find:
	/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp: no matching architecture in universal wrapper
	/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp: no matching architecture in universal wrapper). Root cause UnsatisfiedLinkError (/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp: dlopen(/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp, 1): no suitable image found.  Did find:
	/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp: no matching architecture in universal wrapper
	/Users/nate/Library/Caches/JNA/temp/jna12712874903713535509.tmp: no matching architecture in universal wrapper)
2021-09-20 10:50:14.122 ERROR [api,,] 52788 --- [           main] o.t.d.DockerClientProviderStrategy       : As no valid configuration was found, execution cannot continue
2021-09-20 10:50:14.133 ERROR [api,,] 52788 --- [           main] o.s.boot.SpringApplication               : Application run failed

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.base/java.util.Optional.orElseThrow(Optional.java:408)
	at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:207)
	at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:135)
	at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:177)
	at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14)
	at org.testcontainers.LazyDockerClient.authConfig(LazyDockerClient.java:12)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:314)
	at com.cb.db.DbContainerInitializer.initialize(RepositoryTest.kt:96)

I tried explicitly adding jna dependencies but they didn’t help:

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.9.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>5.9.0</version>
    <scope>test</scope>
</dependency>

Update: After pulling from our main branch, this started working again. I haven’t narrowed it down but I think that org.springframework.data.spring-data-elasticsearch was pulling in an old/bad version of JNA and we upgraded that dependency (it’s now on 7.13.4)

@rdehuyss it is really a question to Gradle and not Testcontainers 😃

Someone pointed out that visible-assertions also depends on JNA (5.2.0) and that some build systems for some reason prioritize it over docker-java-transport: https://github.com/testcontainers/testcontainers-java/issues/4008#issuecomment-824617867

To help with that, since visible-assertions isn’t used by our main classpath anymore, we plan to remove it as a dependency in the next release. You can also try excluding org.rnorth.visible-assertions:visible-assertions already today, just to check whether that’s the case

You don’t need that option with latest versions.

In my case I was seeing the java.lang.UnsatisfiedLinkError: Can't load library: even after upgrading jna lib version to 5.7.0. Further debugging showed that I had another library net.snowflake:snowflake-jdbc.3.12.10 in the class path which had the same jna (com.sun.jna…) code in it. So at run time, my JVM was loading the one from snowflake jar causing the failure. I upgraded to latest net.snowflake:snowflake-jdbc.3.13.19 to resolve the issue. It took me a while to figure out the issue, but was relieved finally 😃

I am seeing the same problem with v1.16.0 and jna v5.8.0

@bsideup indeed, I see it - however, for me it only works if I add the testImplementation 'net.java.dev.jna:jna-platform:5.8.0'. Can’t explain why yet - maybe because I’m using gradle platform to manage my dependencies?

To reproduce, see: https://github.com/jobrunr/jobrunr/blob/silicon-mac/core/build.gradle#L52-L53

Any test that uses TestContainers will fail like LettuceRedisStorageProvider

@bsideup sorry it could be my fault because I wrote two comments on the same issue, I’m talking about the first one https://github.com/testcontainers/testcontainers-java/issues/3810#issuecomment-783221149 you can see there is an error log dropdown. In it appears:

  • 09:54:24.871 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
  • java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

The second comment https://github.com/testcontainers/testcontainers-java/issues/3810#issuecomment-783231622 is a temporary solution. If I run the testcontainers inside Docker I didn’t receive any error but it’s not the best option for development.