带有公司代理的测试容器

Testcontainers with a company proxy

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

这并不奇怪(docker 是在公司代理之后)。如何配置测试容器以使用特定的 HTTP 代理?

另一种方法是禁用 "ping" 命令并使用我们公司的 docker 存储库。

您可以在构建映像或 运行 容器时指定环境变量。例如,下面我通过传递代理配置来构建 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));

否则,您可以在 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

在 Linux,您可以创建文件 ~/.docker/config.json,例如:

{
 "proxies":
 {
  "default":
  {
    "httpProxy": "http://127.0.0.1:3001",
    "noProxy": "*.test.example.com,.example2.com"
  }
 }
}