如何配置 docker/docker-compose 默认使用 Nexus 而不是 docker.io?

问题描述

我正在尝试使用 TestContainers 来运行 JUnit 测试。 但是,我收到了 InternalServerErrorException: Status 500: {"message":"Get https://registry-1.docker.io/v2/: Forbidden"} 错误。

请注意,我使用的是安全网络。

我可以通过在命令行上执行 docker pull testcontainers/ryuk 来复制这一点。

$ docker pull testcontainers/ryuk
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: Forbidden

但是,我需要它从我们的 nexus 服务中提取:https://nexus.company.com/18443。 在 docker-compose 文件中,我已经在使用正确的 nexus 图像路径。 (通过使用 docker-compose 手动启动它来验证。然而,TestContainers 还拉入了 docker-compose 文件之外的其他图像。正是这些图像导致了失败。

我很高兴 Docker 桌面或 TestContainers 配置更改可以为我解决这个问题。

注意:我已经尝试将 nexus 的主机 URL 添加到仪表板上的 Docker 引擎 JSON 配置中,执行 docker pull 时没有更改产生的错误。

解决方法

由于版本 1.15.1 测试容器允许自动将前缀附加到所有 docker 图像。如果您的私有注册表配置为 docker hub 镜像,则此功能应该有助于解决上述问题。

引自documentation

然后,您可以配置 Testcontainers 将前缀 registry.mycompany.com/mirror/ 应用于它尝试从 Docker Hub 拉取的每个镜像。这可以通过以下两种方式之一完成:

  • 设置环境变量 TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/
  • 通过配置文件,将 hub.image.name.prefix 设置为:
    • 用户主目录中的 ~/.testcontainers.properties 文件,或
    • 类路径上名为 testcontainers.properties 的文件

基本上为 docker-compose 文件中的图像设置相同的前缀。

如果您因某种原因坚持使用旧版本,则不推荐使用的解决方案是仅覆盖 ryuk.container.image 属性。阅读它here

,

该过程在 this page 中有描述:

将以下内容添加到您的 Docker 守护程序配置中:

{
  "registry-mirrors": ["https://nexus.company.com:18443"]
}

确保重新启动守护进程以应用更改。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...