首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用docker-java从Amazon中提取图像

使用docker-java从Amazon中提取图像
EN

Stack Overflow用户
提问于 2016-10-18 04:29:43
回答 3查看 2.5K关注 0票数 4

我面临一个问题,从Amazon使用docker客户端提取图像。ECR注册表登录的身份验证是成功的,但无法从存储库中提取特定的图像。奇怪的是,使用bash登录到ECR中并使用docker提取图像是可行的。

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

代码语言:javascript
复制
    // 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(); 

标准是:

代码语言:javascript
复制
    Login Succeeded
    Exception in thread "main" com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unauthorized: authentication required
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-10-23 15:53:28

您需要将客户端的AuthConfig传递给pull命令。

代码语言:javascript
复制
PullImageCmd pullImageCmd = dockerClient
    .pullImageCmd(respositoryname)
    .withAuthConfig(dockerClient.authConfig());
票数 2
EN

Stack Overflow用户

发布于 2019-10-08 21:07:14

对我来说,问题是authData.getEndpointProxy()返回了一个“authData.getEndpointProxy”的URL,但是拉图像cmd只能在没有前缀的情况下工作,所以我不得不删除它。

票数 0
EN

Stack Overflow用户

发布于 2022-07-07 12:51:25

我也有同样的问题,我添加了"withAuthConfig“和"withRepository”,但是我仍然有“未经授权的:不正确的用户名或密码”。知道为什么吗?

我的代码:

代码语言:javascript
复制
public void pullImage() {
    GetAuthorizationTokenRequest request = new GetAuthorizationTokenRequest();
    // Get Authorization Token
    GetAuthorizationTokenResult response = client.getAuthorizationToken(request);
    AuthorizationData authData = response.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);

    DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
            .withDockerTlsVerify(false)
            .withRegistryUsername(user)
            .withRegistryPassword(password)
            .withRegistryUrl(authData.getProxyEndpoint().replace("https://", ""))
            .build();

    DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
            .dockerHost(config.getDockerHost())
            .sslConfig(config.getSSLConfig())
            .maxConnections(100)
            .connectionTimeout(Duration.ofSeconds(30))
            .responseTimeout(Duration.ofSeconds(45))
            .build();

    //Docker client
    DockerClient dockerClient = DockerClientImpl.getInstance(config, httpClient);

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

    // Pull image
    PullImageCmd pullImageCmd = dockerClient
            .pullImageCmd(respositoryname)
            .withAuthConfig(dockerClient.authConfig())
            .withRepository(respositoryname);
    pullImageCmd.exec(new ResultCallback<PullResponseItem>() {
        @Override
        public void onStart(Closeable closeable) {
            System.out.println("Pull started : " + closeable);
        }

        @Override
        public void onNext(PullResponseItem object) {
            System.out.println("Pull on next : " + object);
        }

        @Override
        public void onError(Throwable throwable) {
            System.out.println("Pull error : " + throwable);
        }

        @Override
        public void onComplete() {
            System.out.println("Pull finished");
        }

        @Override
        public void close() throws IOException {
            System.out.println("Pull closed");
        }
    });
}

结果:

代码语言:javascript
复制
Login Succeeded
Pull error : com.github.dockerjava.api.exception.InternalServerErrorException: Status 500: {"message":"Get \"https://registry-1.docker.io/v2/library/pleiade3tierserver/tags/list\": unauthorized: incorrect username or password"}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40099527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档