4

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.

I am using 3.0 version of java-docker library (https://github.com/docker-java/docker-java/). Any help on how to debug or solve this issue will be useful.

    // 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(); 

The stdout is:

    Login Succeeded
    Exception in thread "main" com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unauthorized: authentication required
Lokesh A. R.
  • 2,326
  • 1
  • 24
  • 28

2 Answers2

2

You need to pass the client's AuthConfig into the pull command.

PullImageCmd pullImageCmd = dockerClient
    .pullImageCmd(respositoryname)
    .withAuthConfig(dockerClient.authConfig());
John Zeringue
  • 705
  • 3
  • 13
  • I tried your suggestion. I get the following error: Exception in thread "main" com.github.dockerjava.api.exception.UnauthorizedException: Get https://registry-1.docker.io/v2/library/xyz_repository_name: unauthorized: incorrect username or password – Lokesh A. R. Nov 01 '16 at 15:11
  • 1
    Almost correct answer, i had to add the repository_name too: .withRepository("xyz_repository"); – Lokesh A. R. Nov 01 '16 at 15:33
0

For me the issue was, that authData.getEndpointProxy() returned a URL with "https://" but the pull image cmd only works without that prefix, so I had to remove it.