由于“找不到有效的Docker环境”,测试容器测试用例失败。

92

我对使用测试容器非常陌生。我的测试失败并出现以下异常。

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

我的问题是我是否需要在本地安装Docker才能使用Testcontainers?如果是,那么在像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

更新:我已经在本地安装了Docker(以尝试它),但由于无法下载镜像,仍然无法运行测试用例。我的公司网络可能是一个问题。有没有人对以上情况有好的解决方案?

更新:我已经在本地安装了Docker(以尝试它),但由于无法下载镜像,仍然无法运行测试用例。我的公司网络可能是一个问题。有没有人对以上情况有好的解决方案?

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)

4
是的,Testcontainers需要有效的Docker安装。当涉及到您的第二个问题(在安装了Docker之后):请尝试使用CLI拉取镜像,即运行控制台并执行“docker pull <image name>”,然后发布结果。 - jannis
1
感谢 @jannis。我的公司 VPN 网络似乎不允许下载,我也尝试了 CLI,但是得到了 EOF 异常。 有没有其他方法可以获取镜像,并指示 testcontainers 和/或 docker 离线工作的方式? - Ketan
你需要以某种方式将图像获取到本地注册表中。选项包括:a] 在本地构建图像 b] 从远程注册表拉取(但正如你所写的那样,这不起作用)c] 使用docker save将图像导出到文件,然后使用docker load将其加载到本地注册表中。 - jannis
@jannis,您所说的有效Docker容器是什么意思?我也遇到了同样的问题。在我的情况下,测试容器在本地使用PostgreSQL正常工作,但当我将其推送到GitLab时,我的流水线失败了,并显示相同的错误。 - valik
@valik 我写的是“有效的Docker安装”,而不是“有效的Docker容器”。 - jannis
显示剩余2条评论
16个回答

118
如果你正在使用MAC,针对我的情况,解决我遇到的问题需要添加一个链接。
按照以下命令操作: sudo ln -s $HOME/.docker/run/docker.sock /var/run/docker.sock

8
这个方法适用于我(Mac M1) - code veda
5
也适用于 M1 Pro。 - Inanc Cakil
1
同样适用于英特尔的 Mac 电脑。 - gbear605
3
有一个额外的问题是,每次升级后,Mac似乎会删除这个ln(我不得不为此收藏了这个答案)。 - paulpdaniels
2
在我的情况下(Rancher Desktop),命令是:sudo ln -s $HOME/.rd/docker.sock /var/run/docker.sock - undefined
显示剩余2条评论

58
在我的情况下,这个错误是由于根权限引起的。如果你使用的是ubuntu,docker需要权限来创建容器,而testcontainers无法在运行时向你请求这个权限。所以你需要让docker在没有sudo的情况下可运行。
尝试以下步骤来创建docker组并添加相关用户:
$ sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ sudo service docker restart

如果您使用的是Ubuntu 14.04-15.10,请在第3步尝试以下方法:
$ sudo service docker.io restart

在完成所有步骤后,尝试在终端中使用一些不需要使用sudodocker命令。
$ docker ps

如果您遇到权限错误,请重新启动您的计算机,问题应该会得到解决。

29
除了这些步骤外,我还需要执行sudo chmod 666 /var/run/docker.sock - Amy Doxy
我遇到了这个问题:sudo: groupadd: 命令未找到。 - Ilya Y
@IlyaY 那么你没有运行Ubuntu。Linux发行版之间的组管理命令会有所不同;可以查找文档,从useradd开始,这应该是相当标准的。另一个可能性是您的PATH上没有/sbin,这将隐藏此和许多其他管理命令;但添加它显然很容易,或者只需使用二进制文件/sbin/groupadd的完整路径即可。 - tripleee
3
@AmyDoxy sudo chmod 666 /var/run/docker.sock 太棒了!非常感谢! - ktulinho
请记得重新登录SSH,然后命令才会生效。 - deniro.wang
这个答案对你没用,并且你使用的是macOS?那就看看@Pratham的答案吧。简而言之:Docker首选项 -> 高级 -> "启用默认的Docker套接字(需要密码)" -> 重启Docker -> 快乐的日子! - undefined

17

在网络上没有其他答案适用于我,很可能是因为我的情况有些独特。我希望这个答案能让其他人在这种奇怪的特定场景中意识到问题所在。

因此如果:

  1. 您正在使用Linux操作系统
  2. 您通过IDE运行测试
  3. 您是通过flatpak、snap或任何其他进行沙箱处理的软件包管理器安装IDE的

问题在于应用程序中的软件包管理器对您进行了沙箱处理。如果您在 /var/run/docker.sock(默认位置)有一个套接字,但测试无法获取它,那么就是出现了这个问题。该文件不存在于沙箱内,只存在于主机文件系统中。根据软件包管理器的不同,/var/run 可能会被挂载到沙箱中,请查看特定的文档。解决这个问题最直接的方法是通过tarball或其他方式在沙箱外安装IDE。


1
如果您正在通过Flatpak运行您的IDE(例如Intellij),您可以通过命令行或Flatseal添加一个覆盖,如此处所述https://github.com/flathub/com.jetbrains.IntelliJ-IDEA-Ultimate/issues/22 - otocon
Podman默认是无需root权限的,并且可以与IntelliJ一起使用,只需假装已安装了Docker:ln -s /run/user/$(id -u)/podman/podman.sock /var/run/user/$(id -u)/docker.sock - undefined

10

如果您的Docker守护程序未运行,也会发生此问题。 如果以前可以正常工作,则启动它并重新运行测试即可解决该问题。


1
这个!谢谢。 - undefined

7

可能是以下几个问题之一:

  • 像您所建议的那样,网络连接不稳定
  • 您还没有从终端登录到dockerhub

在我的情况下,我还需要根据此链接的说明将我的用户添加到docker用户组中:

  • 在进行测试之前,我还拉取了镜像,以防万一。
  • 我还需要在初始化TestContainers实例时指定图像名称,如下所示:

enter image description here


永远不要忘记登录到你的DockerHub :) - JeanValjean
1
登录解决了我的问题 :) 谢谢。 - Jamesed
@JeanValjean 我遇到了同样的问题,但是我无法解决。你能帮我吗?https://stackoverflow.com/questions/77122454/cannot-find-docker-environment-for-test-container-mysql-in-spring-boot/77122986?noredirect=1#comment135958859_77122986 - undefined

4
如果您正在使用Docker桌面版,只需在设置中勾选“允许使用默认的Docker套接字”标志即可。 输入图像描述

4

testcontainers 试图通过检查环境变量 DOCKER_HOST 或文件 <user home>/.testcontainers.properties 来自动检测如何连接到 Docker。

在我的情况下(Win10 + Docker Desktop on WSL2),我必须暴露不带 TLS 的 docker 守护程序 tcp://localhost:2375,然后要么:1)将环境变量及其值(在 IDE 或系统中)设置为 DOCKER_HOST=tcp://localhost:2375,要么 2)创建文件 <user home>/.testcontainers.properties 并添加以下行:

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

顺便提一下,如果你要暴露守护进程,最好使用Windows防火墙限制外部访问(这不是讽刺)。


4
我在我的M1 Mac上遇到了类似的异常情况,当我从Docker Desktop切换到Rancher Desktop时。@Wanderson Xesquevixos de Sique建议的方法最终解决了问题,只是符号链接的路径不同:
sudo ln -s ~/.rd/docker.sock /var/run/docker.sock

3

可能你忘记在电脑上启动Docker了。 这对我解决了问题。


3
Testcontainers通过套接字与Docker进行通信。在较新版本的Docker Desktop中,默认情况下禁用了套接字。
可以通过以下步骤启用:首选项 -> 高级 -> "启用默认的Docker套接字(需要密码)"。

这就是具体解决我的方法!谢谢分享。MacOS开发者们要注意,升级到Sonoma可能会触发Docker自动升级,并且这个标志可能被设置为false,迫使你采用@Pratham提供的解决方案。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接