Ketan
Ketan

Reputation: 3332

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

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)

Upvotes: 141

Views: 211013

Answers (22)

Zaroual Mohamed
Zaroual Mohamed

Reputation: 415

In my case , I have moved from Docker desktop to Rancher Desktop, and this command from @volkovs worked form

sudo ln -s $HOME/.rd/docker.sock /var/run/docker.sock

Upvotes: 2

Jake
Jake

Reputation: 3028

In my case (linux), when docker ps worked but my test containers test threw the IllegalStateException, the solution was setting the DOCKER_HOST env var.

export DOCKER_HOST='unix:///var/run/docker.sock' 

In case it's useful, this is the line in test containers throwing the exception. I debugged backward from there:

https://github.com/testcontainers/testcontainers-java/blob/f7973aa659d5f871c6ee4830d1a786a22b2ee876/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java#L275

which brought me here, where it's configuring its connection to test: https://github.com/testcontainers/testcontainers-java/blob/f7973aa659d5f871c6ee4830d1a786a22b2ee876/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java#L275

So it needed the docker host URI. Which brought me here: https://github.com/testcontainers/testcontainers-java/blob/f7973aa659d5f871c6ee4830d1a786a22b2ee876/core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java#L275

Setting DOCKER_HOST to that URI solved the issue.

Upvotes: 0

Devil
Devil

Reputation: 1

I was facing the same issue. I added the below dependency

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.13.0</version>
</dependency>

Upvotes: 0

Joao Marques
Joao Marques

Reputation: 111

Make sure you have Docker Desktop running locally

Upvotes: -2

Bogdan
Bogdan

Reputation: 89

I am using Colima with multiple profiles and this worked for me:

sudo ln -s $HOME/.colima/{profilename}/docker.sock /var/run/docker.sock

Upvotes: 3

Paul Podgorsek
Paul Podgorsek

Reputation: 2643

For people using a rootless Podman, TestContainers currently only officially support Docker. Given Podman's aim to be a stand-in replacement for Docker, it is however possible to use it with TestContainers:

# Enable the Podman socket
systemctl --user enable podman.socket --now

# This environment variable is used by TestContainers
export DOCKER_HOST=unix:///run/user/`id -u`/podman/podman.sock

# Ryuk needs to be disabled to avoid running root containers in Podman
export TESTCONTAINERS_RYUK_DISABLED=true

The systemctl command only needs to be executed once. (will still work after reboots) The two export commands can be added to your bashrc or similar shell configuration.

Upvotes: 2

If you're using MAC, in my case, to solve the problem I had to add a link.

Follow the command below:

sudo ln -s $HOME/.docker/run/docker.sock /var/run/docker.sock

Upvotes: 183

Emin Bilgic
Emin Bilgic

Reputation: 1311

In my case, this error was caused by root permission. If you are using ubuntu, docker wants permission to create a container and test-containers can not ask you for this permission at runtime. So you need to make docker runnable without sudo.

Try these steps to create a docker group and add a connected user:

$ sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ sudo service docker restart

Try this in step 3 if you are using Ubuntu 14.04-15.10:

$ sudo service docker.io restart

After all, steps try some docker commands in the terminal without sudo like:

$ docker ps

If you are taking permission error, restart your computer, and then this must be fixed.

According to @AmyDoxy's suggestion, in addition to these steps, if the test container still can't find docker run the following command:

$ sudo chmod 666 /var/run/docker.sock

Upvotes: 77

Utkarsh Duvey
Utkarsh Duvey

Reputation: 41

If you are using colima, use

sudo ln -s $HOME/.colima/default/docker.sock /var/run/docker.sock

Upvotes: 4

Anirudh Ramesh
Anirudh Ramesh

Reputation: 138

I am using colima, and only this worked for me(2019 i9 mac).

export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock
export DOCKER_HOST=unix://${HOME}/.colima/docker.sock
sudo ln -s $HOME/.colima/docker.sock /var/run/docker.sock

Upvotes: 6

Carlos Cuesta
Carlos Cuesta

Reputation: 1494

If you are using Docker Desktop, simply check the flag "Allow the default Docker socket to be used" in Settings enter image description here

Upvotes: 10

LarryW
LarryW

Reputation: 219

I had a similar exception on my M1 Mac, when switching from Docker Desktop to Rancher Desktop. The approach suggested by @Wanderson Xesquevixos de Sique turned out to solve things, except that the path for symbolic linkage turned out to be different:

sudo ln -s ~/.rd/docker.sock /var/run/docker.sock

Upvotes: 13

Pratham
Pratham

Reputation: 1723

Testcontainers communicate to docker via sockets. In the newer version of Docker Desktop sockets are disabled by default.

This can be enabled Preferences -> Advanced -> "Enable default Docker socket (Requires password)"

Upvotes: 7

lilgallon
lilgallon

Reputation: 625

None of the solutions listed here did not work for me. I installed docker desktop, and docker.sock was not in /var/run/docker.sock.

So I had to find docker.sock using ss -x -a | grep docker.sock:

u_str LISTEN 0      4096                     /home/xxx/.docker/desktop/docker.sock 37055              * 0             
u_str ESTAB  0      0                        /home/xxx/.docker/desktop/docker.sock 5890598            * 5891857       
u_str ESTAB  0      0                        /home/xxx/.docker/desktop/docker.sock 44658              * 49293  

I found that the file was in $HOME/.docker/desktop/docker.sock.

So to make it work, I had to run sudo ln -s $HOME/.docker/desktop/docker.sock /var/run/docker.sock

Hope it helps you :)

Upvotes: 3

mnagdev
mnagdev

Reputation: 524

In my case, Docker Desktop wasn't able to pull images because the CPU usage was 100%. Restarting the PC fixed my issue,

Upvotes: 0

Rafał Borkowski
Rafał Borkowski

Reputation: 93

I had the same problem but only when I try to debug code. In my case the problem was too many breakpoints in Intellij. When I deleted all of them (especially from previous debuggings) and then put only one which I needed, the test container started normally. Maybe my response can be helpful in case when any other options don't work.

Upvotes: 0

Maybe you forgot start docker on your computer. This solved the problem for me.

Upvotes: 5

The official documentation will help you.

$ dockerd-rootless-setuptool.sh install

Upvotes: 3

Nelson Garcia
Nelson Garcia

Reputation: 321

testcontainers tries to auto-detect how to connect to docker by inspecting the environment variable DOCKER_HOST or the file <user home>/.testcontainers.properties

In my case (Win10 + Docker Desktop on WSL2), I had to expose the docker daemon on tcp://localhost:2375 without TLS and then either: 1) set the environment variable and its value (in IDE or system) to DOCKER_HOST=tcp://localhost:2375 or 2) create the file <user home>/.testcontainers.properties and add this lines:

docker.host=tcp\://localhost\:2375
# This second line might be not completely necessary:
docker.client.strategy=org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy

BTW, if you expose the daemon, it would be better if you restrict access to it from outside your machine, using the Windows Firewall (it's not sarcasm ;-) )

Upvotes: 7

Anand
Anand

Reputation: 743

This issue also happens when your docker demon is not running. Starting it and running the tests again would fix the issue if it used to work before.

Upvotes: 11

MJC
MJC

Reputation: 216

No other answers across the internet worked for me, likely because my case was somewhat unique. I am hoping this answer allows others in this weird, particular scenario to realize what is the problem.

So If:

  1. You are on Linux
  2. You are running your tests through an IDE
  3. You installed IDE through flatpak, snap, or any other package manager that does sandboxing

The issue is that you are sandboxed by the package manager in the application. If you have a socket at /var/run/docker.sock (default), but the tests can't pick it up, this is why. That file does not exist in the sandbox, only on the host filesystem. Depending on the package manager, /var/run may be mounted inside the sandbox, so check your particular documentation. The most straightforward way to resolve this is to install the IDE outside of a sandbox via a tarball or some other thing.

Upvotes: 19

Joe Seff
Joe Seff

Reputation: 1830

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

Upvotes: 8

Related Questions