Test Container test cases are failing due to "Could not find a valid Docker environment"

Ketan picture Ketan · Apr 8, 2020 · Viewed 8.8k times · Source

I am very new to using test containers. My test is failing with below exception.

Running com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest
2020-04-08 14:27:08.441  INFO   --- [           main] o.s.t.c.support.AbstractContextLoader    
: Could not detect default resource locations for test class 
resource found for suffixes {-context.xml, Context.groovy}.
2020-04-08 14:27:08.449  INFO   --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest]: DemoControllerTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2020-04-08 14:27:08.611  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration com.mastercard.example.testcontainers.testcontainersexampple.TestContainersExampleApplication for test class com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest
2020-04-08 14:27:08.701  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2020-04-08 14:27:08.725  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@117159c0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3e27ba32, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@7ef82753, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@3b0fe47a, org.springframework.test.context.support.DirtiesContextTestExecutionListener@202b0582, org.springframework.test.context.transaction.TransactionalTestExecutionListener@235ecd9f, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1ca3b418, org.springframework.test.context.event.EventPublishingTestExecutionListener@58cbafc2, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2034b64c, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@75d3a5e0, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@74d1dc36, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@7161d8d1, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@74e28667]
2020-04-08 14:27:08.781 ERROR   --- [           main] o.t.d.DockerClientProviderStrategy       : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2020-04-08 14:27:08.782 ERROR   --- [           main] o.t.d.DockerClientProviderStrategy       :     UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed). Root cause NoSuchFileException (/var/run/docker.sock)
2020-04-08 14:27:08.782 ERROR   --- [           main] o.t.d.DockerClientProviderStrategy       : As no valid configuration was found, execution cannot continue

My question is do I need docker installed locally in order to use testcontainers? If yes, how does this works from pipeline like Jenkins?

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$3(DockerClientProviderStrategy.java:158)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:150)
at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:111)
at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:175)
at org.testcontainers.containers.JdbcDatabaseContainer.<init>(JdbcDatabaseContainer.java:36)
at org.testcontainers.containers.PostgreSQLContainer.<init>(PostgreSQLContainer.java:32)
at com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest.<clinit>(DemoControllerTest.java:27)
... 25 more

UPDATE I have installed docker in local (to try it out) but still I am not able to run test cases due to not able to download image. My corporate network might be an issue here. Does anyone has good solution for above situations?

Caused by: com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unexpected EOF
    at com.github.dockerjava.core.command.PullImageResultCallback.checkDockerClientPullSuccessful(PullImageResultCallback.java:96)
    at com.github.dockerjava.core.command.PullImageResultCallback.throwFirstError(PullImageResultCallback.java:111)
    at com.github.dockerjava.core.async.ResultCallbackTemplate.awaitCompletion(ResultCallbackTemplate.java:91)
    at com.github.dockerjava.core.command.PullImageResultCallback.awaitSuccess(PullImageResultCallback.java:124)

Answer

Joseph Waweru picture Joseph Waweru · Feb 22, 2021

It could be one of several issues:

  • Unstable network connection as you suggested
  • You have not logged in to dockerhub from your terminal

In my case I needed to also add my user to the docker user group as per this link:

  • I also pulled the image before running the test, just in case.
  • One more thing I had to do was to specify the image name when initialising the TestContainers instance as is shown below:

enter image description here