testcontainers-java: "Could not find a valid Docker environment" error while running tests from Jenkins on the Ubuntu

The environment:

  • Host machine: Ubuntu 16.04 with Docker installed and running;
  • Jenkins which is run via Docker container on the same host machine.

I am using testcontainers-java in my project and trying to setup executing SeleniumWebdriver tests via Maven from the Jenkins CI. Example code is taken from the official documentation and works perfectly if I run tests from IDE installed on the same Linux host machine, or if I run tests from a terminal via Maven - testcontainers-java starts an appropriate container and I receive correct data from the browser under test.

However, when I try to run the same tests with the same set of Maven commands from a Jenkins job - I receive the following error:

java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$2(DockerClientProviderStrategy.java:146) at java.util.Optional.orElseThrow(Optional.java:290) at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:138) at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:99) at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:126) at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:166) at org.testcontainers.containers.BrowserWebDriverContainer.<init>(BrowserWebDriverContainer.java:61)

I suspect that there is maybe some issue because Jenkins itself is running inside another container and cannot get access to the installed Docker instance from inside. Is there any way to solve this issue? I was trying to search for a solution a lot but didn’t succeed. This forum is my last hope

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 24 (9 by maintainers)

Most upvoted comments

So, the final solution that works for me:

  1. Add jenkins user to the docker group: sudo usermod -aG docker jenkins
  2. Reboot a host machine. Optionally, you can open the terminal under the jenkins user and run the groups command to make sure that there is the docker group in the list.

After that Jenkins is able to deal with the docker.sock file and TestContainers can see the Docker installation.

In my case, I solved the issue in the following way:

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

Actually, I tried a different approach and found a workaround - I switched to the standalone Jenkins installation (not as a Docker container) and got the same error as described above.

Then I thought that maybe the jenkins user doesn’t doesn’t have all permissions to read the /var/run/docker.sock file, ant it causes all problems. So, as a hotfix I changed permissions for it: chmod 777 /var/run/docker.sock and now TestContainers detects Docker correctly.

I will check later with the Jenkins installation as a Docker container and let you know here. Obviously, permissions should be set for the jenkins user (maybe making it root).

You can also use docker run -v /var/run/docker.sock:/var/run/docker.sock --group-add ${dockerGroupId} ... when starting the container. This will add the group to the user inside the docker container which gives it access to the .sock file.

So, the final solution that works for me:

  1. Add jenkins user to the docker group: sudo usermod -aG docker jenkins
  2. Reboot a host machine. Optionally, you can open the terminal under the jenkins user and run the groups command to make sure that there is the docker group in the list.

After that Jenkins is able to deal with the docker.sock file and TestContainers can see the Docker installation.

I did it right, but forget to reboot 😃 Please, don’t lose your time like me and reboot your host after adding user to docker group.

@jabrena This is not recommended and a considerable security issue. If possible I would recommend to change it again to 660. Also see https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface

Sounds good, great to hear you got it working 🙂

Normally you should add the jenkins user to the docker group (but please also see this: Docker daemon attack surface)

Since this is nothing Testcontainers related, I’ll close this issue. If you have any more questions regarding how to setup your CI to best leverage Docker and Testcontainers, simply ask in our Slack channel, we try to help as best as we can 🙂

@endless-qa please configure the logs as recommended here: https://www.testcontainers.org/usage.html#logging

Do not forget to add Slf4j-compatible logging implementation (we recommend Logback)

This link is pointing here now