Pushing a local docker image to Amazon ECR Repository using .NET 5, C# and AWS ECR SDK

Adityanand Pasumarthi
The AWS Coder
Published in
3 min readFeb 22, 2022
Cloud Native Development

Well, this is a small story on how to programmatically push a local docker image to Amazon ECR Repository using .NET 5, C# and AWS ECR SDK.

The working sample is available on GitHub at below address…You need an AWS account to run this sample. This story assumes you know how to create a AWS IAM user for programmatic access using combination of Api Key and Secret Key, and assign required permissions for the user.

https://github.com/apasumarthi1999/DockerAWSECRPushDotNet

Here are few easy steps in which you can achieve this…

In your .NET 5 project, add a reference to Docker.DotNet package. You can find this from your Visual Studio package explorer. Here is a link to the package GitHub site.

https://github.com/dotnet/Docker.DotNet

Add a reference to the AWSSDK.ECR. You can find this from your Visual Studio package explorer.

Assuming you have your docker image ready in your local docker repository, this sample follows the below steps to push it to your AWS ECR repository.

Step 1: Get the name of the docker image as input.

Step 2: Create a new ECR repository on AWS ECR in your AWS account, where the docker image needs to be pushed to. For this, you need to setup your AWS API Key and AWS Secret Key in the sample project’s appsettings.json. These keys need to have the permission to create ECR repositories and push docker images to the ECR in your AWS account. You also need to update the AWS ECR region in which you want to push the docker image.

Step 3: To push your docker image to the above created ECR repository, your docker engine need to authenticate with the Amazon ECR. For this we can generate a authorization token from your AWS ECR keys, which can act as a password for the docker engine when pushing the docker image.

Step 4: Tag your local docker image with the ECR repository Uri that we created in Step 2. So that we can use the ECR repository Uri as repository name while pushing the given docker image to the ECR.

Step 5: Push the docker image with the ECR repository Uri to the corresponding ECR Repository created in Step 2. The user name in the AuthConfig object, which is used for authorization with ECR, should always be “AWS” and the password is the access token obtained in Step 3. The ServerAddress property of the AuthConfig in the below code should be the endpoint we got in the response of the authorization request to ECR (in Step 3).

Now the PushImageAsync method in the above code snippet will await until the push of the docker image is completed. Meanwhile we can track the progress by passing an object of a class that implements IProgress<JSONMessage>interface to the PushImageAsync method. For this, we implemented a place holder class named DockerPushProgress that simply prints the progress messages. You can fine tune this by examining more about the progress object that is coming into the Report method of the implemented class.

Voila, we are done pushing a local docker image to AWS ECR repository programmatically using .NET 5, C# and handy Docker.DotNet package. Happy AWS coding!

--

--