Use PushImageCmd with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use PushImageCmd with an AWS SDK or CLI

The following code examples show how to use PushImageCmd.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/** * Pushes a Docker image to an Amazon Elastic Container Registry (ECR) repository. * * @param repoName the name of the ECR repository to push the image to. * @param imageName the name of the Docker image. */ public void pushDockerImage(String repoName, String imageName) { System.out.println("Pushing " + imageName + " to Amazon ECR will take a few seconds."); CompletableFuture<AuthConfig> authResponseFuture = getAsyncClient().getAuthorizationToken() .thenApply(response -> { String token = response.authorizationData().get(0).authorizationToken(); String decodedToken = new String(Base64.getDecoder().decode(token)); String password = decodedToken.substring(4); DescribeRepositoriesResponse descrRepoResponse = getAsyncClient().describeRepositories(b -> b.repositoryNames(repoName)).join(); Repository repoData = descrRepoResponse.repositories().stream().filter(r -> r.repositoryName().equals(repoName)).findFirst().orElse(null); assert repoData != null; String registryURL = repoData.repositoryUri().split("/")[0]; AuthConfig authConfig = new AuthConfig() .withUsername("AWS") .withPassword(password) .withRegistryAddress(registryURL); return authConfig; }) .thenCompose(authConfig -> { DescribeRepositoriesResponse descrRepoResponse = getAsyncClient().describeRepositories(b -> b.repositoryNames(repoName)).join(); Repository repoData = descrRepoResponse.repositories().stream().filter(r -> r.repositoryName().equals(repoName)).findFirst().orElse(null); getDockerClient().tagImageCmd(imageName + ":latest", repoData.repositoryUri() + ":latest", imageName).exec(); try { getDockerClient().pushImageCmd(repoData.repositoryUri()).withTag("echo-text").withAuthConfig(authConfig).start().awaitCompletion(); System.out.println("The " + imageName + " was pushed to ECR"); } catch (InterruptedException e) { throw (RuntimeException) e.getCause(); } return CompletableFuture.completedFuture(authConfig); }); authResponseFuture.join(); }
  • For API details, see PushImageCmd in AWS SDK for Java 2.x API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/** * Pushes a Docker image to an Amazon Elastic Container Registry (ECR) repository. * * @param repoName the name of the ECR repository to push the image to. * @param imageName the name of the Docker image. */ suspend fun pushDockerImage( repoName: String, imageName: String, ) { println("Pushing $imageName to $repoName will take a few seconds") val authConfig = getAuthConfig(repoName) EcrClient { region = "us-east-1" }.use { ecrClient -> val desRequest = DescribeRepositoriesRequest { repositoryNames = listOf(repoName) } val describeRepoResponse = ecrClient.describeRepositories(desRequest) val repoData = describeRepoResponse.repositories?.firstOrNull { it.repositoryName == repoName } ?: throw RuntimeException("Repository not found: $repoName") val tagImageCmd = getDockerClient()?.tagImageCmd("$imageName", "${repoData.repositoryUri}", imageName) if (tagImageCmd != null) { tagImageCmd.exec() } val pushImageCmd = repoData.repositoryUri?.let { dockerClient?.pushImageCmd(it) // ?.withTag("latest") ?.withAuthConfig(authConfig) } try { if (pushImageCmd != null) { pushImageCmd.start().awaitCompletion() } println("The $imageName was pushed to Amazon ECR") } catch (e: IOException) { throw RuntimeException(e) } } }
  • For API details, see PushImageCmd in AWS SDK for Kotlin API reference.