Create Elastic Container Registry (ECR) and Upload Images

Amazon Elastic Container Registry (ECR) is a fully managed container registry offering high-performance hosting, so you can reliably deploy application images and artifacts anywhere.

Note

Ensure AWS Credentials are properly configured before running any aws cli commands. This ensures that all aws commands will work as expected. For further information, see AWS Credentials Configuration Documentation.

Create Elastic Container Registry

Use the following to create an ECR repository:

aws ecr create-repository --repository-name xxxxxxxxxxxx

{
    "repository": {
        "repositoryArn": "arn:aws:ecr:us-west-2:xxxxxxxxxxxx:repository/dl1_batch_training",
        "registryId": "xxxxxxxxxxxx",
        "repositoryName": "dl1_batch_training",
        "repositoryUri": "xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/dl1_batch_training",
        "createdAt": 1661376423.0,
        "imageTagMutability": "MUTABLE",
        "imageScanningConfiguration": {
            "scanOnPush": false
        },
        "encryptionConfiguration": {
            "encryptionType": "AES256"
        }
    }
}

Build and Push Image to ECR

  1. Create a config.properties file to manage image configuration:

registry=xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com
region=us-west-2
image_name=xxxxxxxxxxxx
image_tag=v1

Field

Description

registry

Update xxxx to match your AWS Account Number

image_name

Update xxxx to the name of your image

  1. Create a build.sh file to build and tag an image based on the properties file:

#!/bin/bash

source ./config.properties

echo "Building base container ..."

docker build -t ${registry}/${image_name}:${image_tag} -f ./Dockerfile .
  1. Run build.sh to build and tag the Docker image:

./build.sh
  1. Create a push.sh file to log into the generated ECR and push the built image to the registry based on the properties file:

#!/bin/bash
source ./config.properties

echo "Logging into ECR"
aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${registry}

echo "Pushing to ECR"
docker push ${registry}/${image_name}:${image_tag}

echo "Done..."
  1. Run push.sh to log into the ECR and push the built image:

./push.sh