2016-10-18 7 views
1

docker-javaクライアントを使用してAmazon ECRから画像を取得する際に問題が発生しています。 ECRレジストリログインの認証は成功しましたが、リポジトリから特定のイメージを取得できません。奇妙なことは、bashを使ってECRにログインし、ドッカーを使ってイメージを引っ張ることです。docker-javaを使用してAmazon ECRから画像を引き出す

私はjava-dockerライブラリ(https://github.com/docker-java/docker-java/)3.0バージョンを使用しています。この問題をデバッグまたは解決する方法についての助けが役に立ちます。

// 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

私はあなたの提案を試みました。次のエラーが表示されます。スレッド "main"の例外com.github.dockerjava.api.exception.UnauthorizedException:Get https://registry-1.docker.io/v2/library/xyz_repository_name:権限のないユーザー名またはパスワードが正しくありません –

+0

ほぼ正しい答え、私もrepository_nameを追加しなければならなかった:.withRepository( "xyz_repository"); –

関連する問題