gpt4 book ai didi

Test Container test cases are failing due to "Could not find a valid Docker environment"(由于“找不到有效的Docker环境”,测试容器测试用例失败)

转载 作者:bug小助手 更新时间:2023-10-25 13:33:59 26 4
gpt4 key购买 nike



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?

我的问题是,为了使用测试容器,我是否需要在本地安装docker?如果是,这是如何从詹金斯这样的管道中实现的?



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?

更新我已经在本地安装了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)

更多回答

Yes, Testcontainers require a valid docker installation. When it comes to your second problem (after you've installed Docker): please try to pull the image with CLI, i.e. run a console and do docker pull <image name> and post the result.

是的,TestContainers需要安装有效的坞站。对于第二个问题(安装Docker后):请尝试使用命令行界面拉取镜像,即运行控制台并进行docker拉取<镜像名称>并发布结果。

Thanks @jannis. It seems my corporate VPN network does not allow to download, I tried from CLI as well and got EOF exception in that. Is there any way to get image otherway and instruct testcontainers and/or docker to work offline?

谢谢@Jannis。我的公司VPN网络似乎不允许下载,我也尝试了CLI,但得到了EOF异常。有没有办法以其他方式获取图像,并指示测试容器和/或码头脱机工作?

You need to get the image to the local registry somehow. Options are: a] build the image locally b] pull from remote registry (but as you wrote this does not work) c] use docker save to export the image to a file and then docker load to load it to local registry.

您需要以某种方式将图像发送到本地注册表。选项包括:a]在本地构建映像b]从远程注册表拉取(但正如您所写的那样,这不起作用)c)使用docker保存将映像导出到文件,然后使用docker Load将其加载到本地注册表。

how do I build the image locally? i presume I need to download somehow to my corporate machine which is stopping me to do that

如何在本地构建映像?我想我需要以某种方式下载到我的公司机器上,这阻止了我这样做

how do I build the image locally? You need a Dockerfile and context files (any jars/scripts needed by the Dockerfile). What is this image? Is it a DB image? Spring boot application?

如何在本地构建映像?您需要Dockerfile和上下文文件(Dockerfile需要的任何JAR/脚本)。这是什么图像?它是一个数据库映像吗?Spring Boot应用程序?

优秀答案推荐

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

如果您使用的是MAC,在我的情况下,为了解决这个问题,我必须添加一个链接。


Follow the command below:

请执行以下命令:


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

Sudo ln-S$HOME/.docker/run/docker.sock/var/run/docker.sock



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

在我的例子中,这个错误是由超级用户权限引起的。如果您使用的是ubuntu,docker希望获得创建容器的权限,而测试容器不能在运行时向您要求此权限。因此,您需要在没有sudo的情况下使docker可运行。


Try this steps to create docker group and add 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:

如果您使用的是Ubuntu 14.04-15.10,请在步骤3中尝试:


$ sudo service docker.io restart

After all steps try some docker command in terminal without sudo like:

完成所有步骤后,在终端中尝试一些不带sudo的docker命令,如下所示:


$ docker ps

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

如果出现权限错误,请重新启动计算机,然后必须修复此问题。



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.

问题是您被应用程序中的包管理器沙箱。如果您在/var/run/docker.sock(默认)有套接字,但测试无法获取它,这就是原因。该文件不存在于沙箱中,仅存在于主机文件系统中。根据包管理器的不同,/var/run可能安装在沙箱中,因此请查看您的特定文档。解决这个问题的最直接的方法是通过tarball或其他方法将IDE安装在沙箱之外。



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.

当你的码头恶魔没有运行时,这个问题也会发生。启动它并再次运行测试可以解决这个问题,如果它以前是有效的。



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:

在我的例子中,我还需要按照以下链接将我的用户添加到docker用户组:



  • 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



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

TestContainers尝试通过检查环境变量DOKER_HOST或文件 /.testtainers.properties来自动检测如何连接到docker


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:

在我的例子中(WSL2上的Win10+Docker Desktop),我必须在没有TLS的情况下公开tcp://localhost:2375上的docker守护进程,然后:1)将环境变量及其值(在IDE或系统中)设置为DOKER_HOST=tcp://localhost:2375;或者2)创建文件 /.testtainers.properties并添加以下行:


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 ;-) )

顺便说一句,如果您公开守护程序,最好使用Windows防火墙限制从计算机外部对它的访问(这不是讽刺;-)



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

也许你忘了在你的电脑上启动docker。这解决了我的问题。



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

测试容器通过套接字与Docker通信。在较新版本的Docker Desktop中,套接字默认情况下是禁用的。


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

这可以启用首选项->高级->“启用默认Docker套接字(需要密码)”



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:

当我从Docker Desktop切换到Rancher Desktop时,我的M1 Mac也有类似的例外。@Wanderson Xesquevixos de sique提出的方法最终解决了问题,只是符号联系的路径被证明是不同的:


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


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

如果您使用的是Docker Desktop,只需勾选设置中的“Allow the Default Docker Socket to be Use”标志



The official documentation will help you.

官方文件会对你有所帮助。


$ dockerd-rootless-setuptool.sh install


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.

这里列出的解决方案没有一个对我不起作用。我安装了docker桌面,而docker.sock不在/var/run/docker.sock中。


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

因此,我必须使用ss-x-a|grep docker.sock找到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.

我发现该文件位于$HOME/.docker/ktop/docker.sock中。


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

因此,要使其正常工作,我必须运行sudo ln-S$HOME/.docker/ktop/docker.sock/var/run/docker.sock


Hope it helps you :)

希望它能帮助您:)



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

我正在使用Colima,只有这一款对我有效(2019 I19Mac)。


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


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.

我也遇到了同样的问题,但只有在我尝试调试代码时才会遇到。在我的例子中,问题是IntelliJ中的断点太多。当我将它们全部删除(特别是从以前的调试中删除),然后只放入我需要的一个时,测试容器正常启动。也许我的回答可以在任何其他选择都不起作用的情况下有所帮助。



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

在我的例子中,Docker Desktop无法获取图像,因为CPU使用率为100%。重新启动电脑解决了我的问题,


更多回答

This worked for me (Mac M1)

这对我来说很有效(Mac M1)

works for M1 Pro too.

也适用于M1 Pro。

Worked for an Intel Mac as well

也为英特尔Mac电脑工作过

Don't forget to restart docker after this (or at least in my case it was needed to make this solution work).

完成此操作后,不要忘记重新启动docker(或者至少在我的情况下,需要重新启动才能使此解决方案起作用)。

One addition is that mac seems to remove this ln between upgrades (I've had to bookmark this answer for that reason)

此外,Mac似乎在两次升级之间删除了这个ln(出于这个原因,我不得不将这个答案添加到书签中)

In addition to these steps, I had to sudo chmod 666 /var/run/docker.sock

除了这些步骤之外,我还必须sudo chmod 666/var/run/docker.sock

i have this problem sudo: groupadd: command not found

我遇到此问题:未找到sudo:groupadd:命令

@IlyaY Then you are not running Ubuntu. The group management commands will differ a bit between Linux distros; look for documentation perhaps starting from useradd which should be reasonably standard. Another possibility is that you don't have /sbin on your PATH, which will hide this and many other administrative commands; but adding it is obviously easy, or just use the full path to the binary /sbin/groupadd

@IlyaY,那么您不是在运行Ubuntu。组管理命令在不同的Linux发行版之间会略有不同;查找文档,可能是从用户添加开始,这应该是合理的标准。另一种可能是您的路径中没有/sbin,这将隐藏此命令和许多其他管理命令;但是添加它显然很容易,或者只使用二进制文件/sbin/groupadd的完整路径

@AmyDoxy sudo chmod 666 /var/run/docker.sock FTW!!! Thanks a lot!

@AmyDoxy sudo chmod 666/var/run/docker.sock ftw!非常感谢!

remember must re-login ssh, then command will take effect

请记住,必须重新登录ssh,命令才会生效

If you're running your IDE (Intellij in my case) via Flatpak, you can add an override, like described here github.com/flathub/com.jetbrains.IntelliJ-IDEA-Ultimate/issues/… via command line or in Flatseal

如果您正在通过flatpak运行您的集成开发环境(在我的例子中是IntelliJ),您可以添加一个覆盖,就像这里描述的github.com/flathub/com.jetbrains.IntelliJ-IDEA-Ultimate/issues/…通过命令行或在Flatseal中

podman is rootless by default and works with intellij if you pretend that you have docker installed: ln -s /run/user/$(id -u)/podman/podman.sock /var/run/user/$(id -u)/docker.sock

默认情况下,Podman是无根目录的,如果您假装安装了docker,则可以使用IntelliJ:ln-S/run/user/$(id-u)/podman/podman.sock/var/run/user/$(id-u)/docker.sock

This! Thank you.

这!谢谢。

Never forget to login to your dockerhub :)

永远不要忘记登录到您的坞站中心:)

Lol logging in solved my issue :) Thanks.

LOL登录解决了我的问题:)谢谢。

@JeanValjean I have the same issue but I couldn't fix it. Can you help me? stackoverflow.com/questions/77122454/…

@JeanValjean我也有同样的问题,但我无法解决它。你能帮帮我吗?STACKOVERFLOW.com/Questions/77122454/…

This is specifically what fixed it for me! Thanks for sharing. Macos developers, be warned that upgrade to Sonoma is likely to trigger docker to upgrade itself and this flag may be set to false, forcing you to go for this solution by @Pratham.

这就是为我修复它的特别之处!感谢您的分享。MacOS开发人员请注意,升级到Sonoma可能会触发docker自我升级,此标志可能被设置为FALSE,迫使您通过@Pratham使用此解决方案。

I think this is due to the latest changes in Docker Desktop. Once you "Enable default Docker socket (requires password)" in Preferences -> Advanced as suggested by @Pratham the link will be created!

我认为这是由于Docker Desktop的最新变化。一旦您按照@Pratham的建议,在Preferences->Advanced中“Enable Default Docker Socket(Enable Default Docker Socket)(Enable Default Docker Socket(Required Password)”),链接就会被创建!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com