skip to Main Content

I am trying to run my spring boot app junit tests inside docker and then create image of that app. Below is my Dockerfile:

FROM adoptopenjdk/openjdk14:alpine-jre as base

COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw -B dependency:resolve-plugins dependency:resolve
COPY src ./src

FROM base as test
CMD ["./mvnw", "test"]

FROM base as build
RUN ./mvnw package

COPY target/testapp-0.0.1-SNAPSHOT.jar testapp.jar

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "testapp.jar"]

Now in some of my unit test cases I am using testcontainers with maven dependency as:-

        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>1.16.0</version>
            <scope>test</scope>
        </dependency>

I am using test containers to up cassandra and redis container while testing.
But somehow the classes that are using testcontainers are failing with exception:

java.lang.IllegalStateException: Previous attempts to find a Docker environment failed. Will not retry. Please see logs and check configuration
        at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:109) ~[testcontainers-1.16.0.jar:na]
        at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:135) ~[testcontainers-1.16.0.jar:na]
        at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:177) ~[testcontainers-1.16.0.jar:na]
        at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14) ~[testcontainers-1.16.0.jar:na]
        at org.testcontainers.LazyDockerClient.authConfig(LazyDockerClient.java:12) ~[testcontainers-1.16.0.jar:na]
        at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:314) ~[testcontainers-1.16.0.jar:na]
        at org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.start(TestcontainersExtension.java:242) ~[junit-jupiter-1.16.0.jar:na]
        at org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.access$200(TestcontainersExtension.java:229) ~[junit-jupiter-1.16.0.jar:na]
        at org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$null$1(TestcontainersExtension.java:59) ~[junit-jupiter-1.16.0.jar:na]
        at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$4(ExtensionValuesStore.java:86) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:205) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.evaluate(ExtensionValuesStore.java:182) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.access$100(ExtensionValuesStore.java:171) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$closeAllStoredCloseableValues$1(ExtensionValuesStore.java:65) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at java.base/java.util.stream.ReferencePipeline$2$1.accept(Unknown Source) ~[na:na]
        at java.base/java.util.concurrent.ConcurrentHashMap$ValueSpliterator.forEachRemaining(Unknown Source) ~[na:na]
        at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source) ~[na:na]
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) ~[na:na]
        at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source) ~[na:na]
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source) ~[na:na]
        at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source) ~[na:na]
        at java.base/java.util.stream.ReferencePipeline.forEach(Unknown Source) ~[na:na]
        at org.junit.jupiter.engine.execution.ExtensionValuesStore.closeAllStoredCloseableValues(ExtensionValuesStore.java:68) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.descriptor.AbstractExtensionContext.close(AbstractExtensionContext.java:74) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.execution.JupiterEngineExecutionContext.close(JupiterEngineExecutionContext.java:53) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.cleanUp(JupiterTestDescriptor.java:222) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.cleanUp(JupiterTestDescriptor.java:57) ~[junit-jupiter-engine-5.7.0.jar:5.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$cleanUp$9(NodeTestTask.java:155) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.cleanUp(NodeTestTask.java:155) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:87) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at java.base/java.util.ArrayList.forEach(Unknown Source) ~[na:na]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) ~[junit-platform-engine-1.7.0.jar:1.7.0]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:150) ~[surefire-junit-platform-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124) ~[surefire-junit-platform-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]

Although everything works perfectly fine when I don’t run it inside docker but on my local.

2

Answers


  1. You probably didn’t add your user to the docker group, try:

    sudo usermod -aG docker $USER
    

    Log out, log in again. Check the user has the appropriate groups and launch your app again.

    Login or Signup to reply.
  2. I just ran into the exact same problem.

    Restart your computer, make sure that docker had finished starting up, rerun the test.

    If you want to check that docker is properly running you can try starting up a postgres db in docker with:

    docker run --name postgres-db -e POSTGRES_PASSWORD=docker -p 5432:5432 -d postgres
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search