2016-10-18 43 views
1

我正在使用docker-java客戶端從Amazon ECR中提取圖像時遇到問題。 ECR註冊登錄身份驗證成功,但無法從存儲庫中提取特定映像。奇怪的是,登錄到ECR使用bash和使用碼頭工作拉圖像。使用docker-java從Amazon ECR中提取圖像

我正在使用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(); 

的stdout是:

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

回答

1

您需要將客戶的AuthConfig到拉命令。

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

我試過了你的建議。我得到以下錯誤:線程「主」com.github.dockerjava.api.exception.UnauthorizedException異常:獲取https://registry-1.docker.io/v2/library/xyz_repository_name:未授權:不正確的用戶名或密碼 –

+0

幾乎正確的答案,我也必須添加repository_name:.withRepository(「xyz_repository」); –