Difference between revisions of "Pushing Docker Image To AWS"

Jump to navigation Jump to search
(No difference)

Latest revision as of 23:16, 2 December 2021

Installing the aws cli

The aws cli is required to push a docker image to aws. With these instructions, you can use either version of the cli, however the latest version (cli v2) is always preferable. Here's how to install the cli v2 with conda:

You need to have conda AND docker installed on the machine for this method to work.

conda create -n aws -c conda-forge awscliv2
conda activate aws

Now you should be able to use cli commands.

The conda method of installation is more portable since it uses docker to run the aws cli, however you can also install the cli directly. Follow the instructions here: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html to do this.

Required variables

In the next section, I will be referring to some variables used by AWS. These are AWS_ACCOUNT_ID, and AWS_REGION.

You can discover your AWS_ACCOUNT_ID by running

aws sts get-caller-identity

And inspecting the "UserId" field in the output of this command.

Your AWS_REGION depends on where you would like your aws resources to be used. Generally this should be as close to your current geographic location as possible. You can also decide to utilize multiple regions to maximize your throughput from AWS, for example if you are using AWS batch and would like to run as many concurrent jobs as possible.

You can set your aws region by using "aws configure", and can retrieve your current region with "aws configure get region"

Building and pushing a docker image

The first thing to do is to build your docker image (if it is not already built).

docker build -t $IMAGE_NAME:$IMAGE_TAG .

If your image already exists elsewhere (e.g on dockerhub), you can instead pull it. The important thing is that you have the desired image installed on your machine.

You can view all docker images installed on your machine by running "docker images"

Now we need to use the aws cli to create an empty repository. An ECR repository should be created for each aws region you are utilizing.

aws ecr create-repository --region $AWS_REGION --repository-name ${IMAGE_NAME}_${IMAGE_TAG}

We need to create a version of our image with a special name so we can push it to ECR. ECR does not support image tags the same way dockerhub does, so instead of assigning a tag for our image with a colon, we include the tag as part of the name. This means to upload an image with the same name but different tag it will be necessary to create a new blank repository on ECR.

docker tag $IMAGE_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/${IMAGE_NAME}_${IMAGE_TAG}

Now we need to log in to our ECR account with docker. This can be done with the following command:

aws ecr get-login-password | docker login --password-stdin --username AWS $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

Voila, we are now ready to push the image to ECR.

docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/${IMAGE_NAME}_${IMAGE_TAG}