title: Pulling image from Amazon ECR using docker-java
body: <p>I am facing an issue with pulling image from Amazon ECR using docker-java client. The authentication of ECR registry login is successful, but unable to pull a specific image from the repository. Strange thing is that logging into ECR using bash and pulling image using docker works.</p>
  
  <p>I am using 3.0 version of java-docker library (<a href="https://github.com/docker-java/docker-java/" rel="nofollow">https://github.com/docker-java/docker-java/</a>). Any help on how to debug or solve this issue will be useful.</p>
  
  <pre><code>    // ECR client
      AmazonECRClient ecrClient = new AmazonECRClient(awsCredentialsProvider);
      GetAuthorizationTokenRequest getAuthTokenRequest = new GetAuthorizationTokenRequest();
      List&lt;String&gt; registryIds = new ArrayList&lt;String&gt;();
      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(); 
  </code></pre>
  
  <p>The stdout is:</p>
  
  <pre><code>    Login Succeeded
      Exception in thread "main" com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unauthorized: authentication required
  </code></pre>