gpt4 book ai didi

java - 使用 docker-java 从 Amazon ECR 拉取镜像

转载 作者:行者123 更新时间:2023-11-30 08:32:32 25 4
gpt4 key购买 nike

我在使用 docker-java 客户端从 Amazon ECR 拉取图像时遇到问题。 ECR注册中心登录认证成功,但无法从仓库中拉取特定镜像。奇怪的是,使用 bash 登录 ECR 并使用 docker 提取图像是可行的。

我正在使用 3.0 版的 java-docker 库 ( https://github.com/docker-java/docker-java/ )。任何有关如何调试或解决此问题的帮助都会很有用。

    // ECR client
AmazonECRClient ecrClient = new AmazonECRClient(awsCredentialsProvider);
GetAuthorizationTokenRequest getAuthTokenRequest = new GetAuthorizationTokenRequest();
List<String> registryIds = new ArrayList<String>();
registryIds.add("accountid");
getAuthTokenRequest.setRegistryIds(registryIds);

// Get Authorization Token
GetAuthorizationTokenResult getAuthTokenResult = ecrClient.getAuthorizationToken(getAuthTokenRequest);
AuthorizationData authData = getAuthTokenResult.getAuthorizationData().get(0);
String userPassword = StringUtils.newStringUtf8(Base64.decodeBase64(authData.getAuthorizationToken()));
String user = userPassword.substring(0, userPassword.indexOf(":"));
String password = userPassword.substring(userPassword.indexOf(":")+1);

DockerClientConfigBuilder config = new DockerClientConfigBuilder();
config.withDockerHost("unix:///var/run/docker.sock");
config.withDockerTlsVerify(false);
config.withRegistryUsername(user);
config.withRegistryPassword(password);
config.withRegistryUrl(authData.getProxyEndpoint());
config.build();

DockerCmdExecFactory dockerCmdExecFactory = new DockerCmdExecFactoryImpl();
//Docker client
DockerClient dockerClient = DockerClientBuilder.getInstance(config)
.withDockerCmdExecFactory(dockerCmdExecFactory)
.build();

// Response
AuthResponse response = dockerClient.authCmd().exec();
System.out.println(response.getStatus());

// Pull image
PullImageCmd pullImageCmd = dockerClient.pullImageCmd(respositoryname);
pullImageCmd
.exec(new PullImageResultCallback())
.awaitSuccess();

标准输出是:

    Login Succeeded
Exception in thread "main" com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unauthorized: authentication required

最佳答案

需要将客户端的AuthConfig传入pull命令。

PullImageCmd pullImageCmd = dockerClient
.pullImageCmd(respositoryname)
.withAuthConfig(dockerClient.authConfig());

关于java - 使用 docker-java 从 Amazon ECR 拉取镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com