Skip to main content

Uploading Container Images to Amazon ECR

Objective

This lab shows the process of pushing Docker images to Amazon ECR using the FastAPI and PostgreSQL images from our python-fastapi-demo-docker project. We'll showcase how uploading these Docker images to ECR enhances your development, testing, and deployment workflows by making the images accessible across different environments.

Prerequisites

Initial Setup

Navigate to the root directory of the python-fastapi-demo-docker project where your environment variables are sourced:

cd ~/environment/python-fastapi-demo-docker

1. Creating an ECR Repository

Create a new private Amazon ECR repository:

aws ecr create-repository --repository-name fastapi-microservices

2. Logging into Amazon ECR

Authenticate your Docker CLI to your Amazon ECR registry using:

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

Alternatively, if you're using Finch, run the following command to authenticate:

aws ecr get-login-password \
--region ${AWS_REGION} | finch login \
--username AWS \
--password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com

You should see the following response output: “Login Succeeded”.

Note: If you get an error, check the value of parameter "credsStore" in your docker configuration (e.g., ~/.docker/config.json on Mac). If the value is "ecr-login" you can skip this step, because there is no need to execute the docker login command.

3. Uploading Docker Images to ECR

Tag your Docker image for the ECR repository:

docker tag fastapi-microservices:${IMAGE_VERSION} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}

Alternatively, if you're using Finch, run the following command to tag your image:

finch tag fastapi-microservices:${IMAGE_VERSION} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}

Push the tagged image to the ECR repository:

docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}

Alternatively, if you're using Finch, run the following command to push the image:

finch push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}

4. Retrieving the Docker Image from ECR

Retrieve the Docker image from your ECR repository with this command:

docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}

Alternatively, if you're using Finch, run the following command to retrieve the container image from your ECR repository:

finch pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/fastapi-microservices:${IMAGE_VERSION}

Look for an output message stating that the image is up-to-date, signaling a successful operation.

Conclusion

This lab walked you through the process of pushing a Docker container image to Amazon ECR. This method provides a convenient way to manage and distribute Docker images, making it an essential tool for any developer working with Docker.