如何在不同主机上运行测试容器

Mic*_*ing 3 windows macos docker testcontainers

当启动大量测试容器时,我的 16GB MBP 变得非常慢。我想在运行 Windows 的强大桌面上运行它们。如果这可以在我的本地网络之外工作,那就更好了。

Mic*_*ing 9

首先,在两台机器上安装VPN(例如Tailscale)。您需要这样做,因为 Testcontainers 使用随机端口,因此您不能只重定向您使用的端口,而是需要能够完全访问接口上的所有可能的端口。

客户

然后在使用 Testcontainers 调用测试的客户端上,编辑一些文件。

在 ~/.docker-java.properties 或 C:/Users/myuser/.docker-java.properties 中:

DOCKER_HOST=tcp://DOCKER_HOST_VPN_IP:2375
DOCKER_TLS_VERIFY=0
Run Code Online (Sandbox Code Playgroud)

在 ~/.testcontainers.properties 或 C:/Users/myuser/.testcontainers.properties 中:

docker.client.strategy=org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy
Run Code Online (Sandbox Code Playgroud)

服务器

在运行 Docker 容器的机器上:

在 Docker Desktop 的“设置 > 常规”中启用“Expose daemon”。

在 Windows 上:

在提升的命令提示符下运行,将数据包从 Tailscale IP 转发到本地 Docker: netsh interface portproxy add v4tov4 listenport=2375 connectaddress=127.0.0.1 connectport=2375 listenaddress=<WINDOWS_VPN_IP> protocol=tcp

禁用专用网络的防火墙。

在 Linux 上:

用于IPTABLES将端口 2375 上传入的流量路由到 127.0.0.1:

iptables -t nat -A PREROUTING -p tcp --dport 2375 -j DNAT --to-destination 127.0.0.1:2375
iptables -t nat -A POSTROUTING -j MASQUERADE.
Run Code Online (Sandbox Code Playgroud)

参考

[2] https://github.com/docker-java/docker-java/blob/master/docs/getting_started.md

[2] https://www.testcontainers.org/features/configuration/