如何覆盖 testcontainer 使用的图像注册表 URL

esw*_*aat 5 go testcontainers go-testing

尝试通过测试容器拉取图像时出现错误 [ERROR]。原因 您组织中的 CI 计算机可以访问公共注册表服务器,但不允许与外部 Web 通信。Testcontainer java对于这个用例有类似的东西

Private registry image name

// Referring directly to an image on a private registry - image name will vary
final MySQLContainer<?> mysql = new MySQLContainer<>(
    DockerImageName.parse("registry.mycompany.com/mirror/mysql:8.0.24")
                   .asCompatibleSubstituteFor("mysql")
)
Run Code Online (Sandbox Code Playgroud)

相当于 testcontainer-go 使用的覆盖图像注册表 url 的 go 是什么?

代码

req := testcontainers.ContainerRequest{
        Image: "amazon/dynamodb-local:1.15.0",
        ExposedPorts: []string{"8000" + "/tcp"},
        ReaperImage: artifact_path,
    }
    
    container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
        ContainerRequest: req,
        // auto-start the container
        Started: true,
    })
Run Code Online (Sandbox Code Playgroud)

[错误]

2021/09/28 20:21:11 Failed to pull image: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers), will retry
Run Code Online (Sandbox Code Playgroud)

mde*_*nya 0

您是否尝试过将完全限定的映像名称放入容器请求中并传递您的注册表凭据?

func TestFoo(t *testing.T) {
    authConfig := types.AuthConfig{
        Username: os.Getenv("DOCKER_USER"),
        Password: os.Getenv("DOCKER_PWD"),
    }
    json, err := json.Marshal(authConfig)
    assert.Nil(t, err)

    req := testcontainers.ContainerRequest{
        Image:        "docker.my-company.org/my-namespace/dynamodb-local:1.15.0",
        ExposedPorts: []string{"8000" + "/tcp"},
        RegistryCred: base64.URLEncoding.EncodeToString(json),
    }

    container, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
        ContainerRequest: req,
        // auto-start the container
        Started: true,
    })
    assert.Nil(t, err)
    assert.NotNil(t, container)
}
Run Code Online (Sandbox Code Playgroud)

据我在代码中看到,没有像Java版本中那样的替换函数,但我认为这没有意义,因为在Java中他们有MySQL容器,与Go版本相反,Go版本没有专门的容器。因此,在 Java 版本中,替换 mysql 容器的默认镜像是有意义的。

2023 年 10 月 11 日更新:有一个开放 PR 为 testcontainers-go 提供图像名称替换器: https ://github.com/testcontainers/testcontainers-go/pull/1719

希望这会有所帮助