5

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

2 Answers 2

3

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

PullImageCmd pullImageCmd = dockerClient
    .pullImageCmd(respositoryname)
    .withAuthConfig(dockerClient.authConfig());
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your suggestion. I get the following error: Exception in thread "main" com.github.dockerjava.api.exception.UnauthorizedException: Get registry-1.docker.io/v2/library/xyz_repository_name: unauthorized: incorrect username or password
Almost correct answer, i had to add the repository_name too: .withRepository("xyz_repository");
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.