Pulling image from Amazon ECR using docker-java
amazon-web-services, docker, docker-java, java
Solution
You need to pass the client's `AuthConfig` into the pull command.
PullImageCmd pullImageCmd = dockerClient
.pullImageCmd(respositoryname)
.withAuthConfig(dockerClient.authConfig());
Problem
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 ```