使用公司代理测试容器

ner*_*er0 4 java http-proxy docker docker-java testcontainers

不同测试容器的每次启动都会抛出com.github.dockerjava.api.exception.InternalServerErrorException: {"message":"Get https://quay.io/v1/_ping: dial tcp x.x.x.x: getsockopt: connection refused"}

这并不奇怪(docker 背后有一个公司代理)。如何配置 testcontainers 使用特定的 HTTP 代理?

另一种方法可能是禁用“ping”命令并​​使用我们公司的 docker 存储库。

vic*_*let 5

您可以在构建映像或运行容器时指定环境变量。例如,下面我通过传递代理配置来构建 Elasticsearch 容器:

GenericContainer container = new GenericContainer("docker.elastic.co/elasticsearch/elasticsearch:6.1.1")
            .withExposedPorts(9200)
            .withEnv("discovery.type", "single-node")
            .withEnv("HTTP_PROXY", "http://127.0.0.1:3001")
            .withEnv("HTTPS_PROXY", "http://127.0.0.1:3001")
            .waitingFor(Wait.forHttp("/_cat/health?v&pretty")
                    .forStatusCode(200));
Run Code Online (Sandbox Code Playgroud)

否则,您可以在 docker 中全局设置代理设置。对于带有 docker 机器的 Windows,您必须连接到它以及boot2docker配置文件中的 HTTP 代理。

docker-machine ssh default

sudo -s

echo "export HTTP_PROXY=http://your.proxy" >> /var/lib/boot2docker/profile
echo "export HTTPS_PROXY=http://your.proxy" >> /var/lib/boot2docker/profile
Run Code Online (Sandbox Code Playgroud)

在 Linux 上,您可以创建一个如下文件~/.docker/config.json

{
 "proxies":
 {
  "default":
  {
    "httpProxy": "http://127.0.0.1:3001",
    "noProxy": "*.test.example.com,.example2.com"
  }
 }
}
Run Code Online (Sandbox Code Playgroud)