-
Notifications
You must be signed in to change notification settings - Fork 255
Description
Outline 8. Build & Push Docker Image to AWS ECR
Build & Push Docker Image to AWS ECR
In this chapter, we describe how to build a Docker image using Docker Compose and push it to an AWS ECR repository. Note: The Docker Compose file (aws-hummingbird-docker-compose.yaml) shown below is taken directly from the first chapter's example codes.
Table of Contents
1. Building the Docker Image
Instead of using the traditional docker build command, we now use Docker Compose with a dedicated file named aws-hummingbird-docker-compose.yaml. This file defines the service hummingbird-todos along with the build context, Dockerfile, ports, environment variables, and command.
Below is the content of the aws-hummingbird-docker-compose.yaml file:
services:
hummingbird-todos:
platform: linux/amd64
build:
context: .
dockerfile: hummingbird-docker/Dockerfile
ports:
- "8080:8080"
environment:
DATABASE_HOST: test-database-instance.cgc65h5r3cov.eu-central-1.rds.amazonaws.com
DATABASE_PORT: 5432
DATABASE_USER: postgres
DATABASE_PASSWORD: postgres
DATABASE_NAME: postgres
ROOT_CERT_PATH: eu-central-1-bundle.pem
command: ["--hostname", "0.0.0.0", "--port", "8080"]To build the Docker image using this configuration, run the following command:
docker compose -f aws-hummingbird-docker-compose.yaml buildThis command reads the compose file, builds the image as defined (using the specified Dockerfile and build context), and prepares the container configuration including ports and environment variables.
After building the image, list all Docker images to verify that the newly built image is named with the folder prefix (e.g. <folder-name>-hummingbird-todos):
docker image ls2. Creating the AWS ECR Repository
Before tagging and pushing your Docker image, you need to create a repository on AWS ECR.
- Using the AWS Console:
- Navigate to Amazon Elastic Container Registry and select Create repository.
- Using the CLI:
- Execute the following command (we are using
web-service-repoas the repository name):
- Execute the following command (we are using
aws ecr create-repository --repository-name web-service-repo- Obtain the Repository URI:
- After creation, copy the repository URI (e.g.,
163008249569.dkr.ecr.eu-central-1.amazonaws.com/web-service-repo).
- After creation, copy the repository URI (e.g.,
3. Tagging and Pushing the Docker Image
- Tag the Local Image:
- Tag your local image using the repository URI. Replace
<repository_uri>with your actual repository URI:
- Tag your local image using the repository URI. Replace
docker tag <my-local-image-name>:latest <repository_uri>:latest- Example:
docker tag hummingbird-todos:latest 163008249569.dkr.ecr.eu-central-1.amazonaws.com/web-service-repo:latest- Authenticate with AWS ECR:
- Log in to AWS ECR with the following command. Replace
<region>and<registry_uri>appropriately (note that<registry_uri>is the repository URI without the path):
- Log in to AWS ECR with the following command. Replace
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <registry_uri>- Example:
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin 163008249569.dkr.ecr.eu-central-1.amazonaws.com- Push the Docker Image:
- Finally, push the tagged image to your AWS ECR repository:
docker push <repository_uri>:latest- Example:
docker push 163008249569.dkr.ecr.eu-central-1.amazonaws.com/web-service-repo:latestTip: You can also find these push commands in the AWS Console under Amazon Elastic Container Registry -> Repositories -> select your repository -> View push commands. Note that the commands shown there might use docker build instead of docker compose build.
With these steps, you have successfully built your Docker image using Docker Compose and pushed it to AWS ECR.