使用Testcontainers启动本地Docker Registry后无法推送镜像的问题求助
Testcontainers启动本地Docker Registry后无法推送镜像的问题求助
我现在想用Testcontainers创建一个本地Docker Registry,然后通过代码推送镜像上去,但尝试推送的时候一直报连接拒绝的错误。第一个检查Registry是否运行的测试是正常的,所以我确定Registry是启动起来了的。
以下是我的测试类代码:
class DockerRegistryTest { @Rule public static GenericContainer registry; private static String registryAddress; private static DockerClient dockerClient; @BeforeAll static void startRegistry() { registry = new GenericContainer(DockerImageName.parse("registry:2")) .withExposedPorts(5000) .waitingFor(Wait.forHttp("/v2/").forStatusCode(200)); registry.start(); assertTrue(registry.isRunning(), "Registry is running"); registryAddress = registry.getHost() + ":" + registry.getMappedPort(5000); System.out.println("Registry available at: " + registryAddress); DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build(); dockerClient = DockerClientImpl.getInstance(config, new ApacheDockerHttpClient.Builder() .dockerHost(config.getDockerHost()) .sslConfig(config.getSSLConfig()) .build()); } @AfterAll static void tearDown() { if (registry != null) { registry.stop(); } } @Test void testPushImageToRegistry() throws InterruptedException { String localImage = "busybox:latest"; dockerClient.pullImageCmd(localImage).start().awaitCompletion(); String registryImageTag = registryAddress + "/busybox:latest"; dockerClient.tagImageCmd(localImage, registryAddress + "/busybox", "latest").exec(); dockerClient.pushImageCmd(registryImageTag) .withAuthConfig(new AuthConfig()) // No authentication needed .start() .awaitCompletion(); System.out.println("Successfully pushed image to registry: " + registryImageTag); assertTrue(true); } }
运行测试时,我遇到了这个错误:
com.github.dockerjava.api.exception.DockerClientException: Could not push image: failed to do request: Head "https://localhost:57244/v2/busybox/blobs/sha256:31311c5853a22c04d692f6581b4faa25771d915c1ba056c74e5ec82606eefdfa": dial tcp [::1]:57244: connect: connection refused
我尝试过的操作
- 手动给镜像打标签并推送到这个Registry,结果还是报连接拒绝的错误
- 我执行了curl命令测试Registry状态:
C:\Users\codex>curl http://localhost:<日志中获取的映射端口>/v2/_catalog {"repositories":[]}
能正常返回结果,说明仓库确实在正常运行
- 我还在
testcontainers.properties中添加了这一行配置,确保测试容器能正常工作:
ryuk.container.image=testcontainersofficial/ryuk
这是Docker Desktop的截图:
更新1:
我把代码中的registryAddress赋值语句改成了这样:
registryAddress = "0.0.0.0" + ":" + registry.getMappedPort(5000);
但现在又出现了新的错误:
com.github.dockerjava.api.exception.DockerClientException: Could not push image: failed to do request: Head "https://0.0.0.0:60075/v2/busybox/blobs/sha256:9c0abc9c5bd3a7854141800ba1f4a227baa88b11b49d8207eadc483c3f2496de": http: server gave HTTP response to HTTPS client
备注:内容来源于stack exchange,提问作者twerpiebird
相关产品推荐
相关产品推荐

