Amazon Elastic Compute (EC2) is a service by Amazon that allows us to create virtual machines in the cloud. Amazon Elastic Container Registry (ECR) is also a cloud service by AWS that allows the storage of Docker images.
If most of your cloud services run on AWS, one of the primary advantages of using ECR is that it ensures faster pulls of Docker images.
In this tutorial, we will learn how to deploy a Docker image from ECR to an EC2 instance.
Prerequisites
- Go 1.25+
- Gin
- Docker
- AWS Account
- Familiarity with launching an EC2 instance
Getting Started
Create and set up an EC2 instance. Let us ensure we are logged in to AWS via the termi…
Amazon Elastic Compute (EC2) is a service by Amazon that allows us to create virtual machines in the cloud. Amazon Elastic Container Registry (ECR) is also a cloud service by AWS that allows the storage of Docker images.
If most of your cloud services run on AWS, one of the primary advantages of using ECR is that it ensures faster pulls of Docker images.
In this tutorial, we will learn how to deploy a Docker image from ECR to an EC2 instance.
Prerequisites
- Go 1.25+
- Gin
- Docker
- AWS Account
- Familiarity with launching an EC2 instance
Getting Started
Create and set up an EC2 instance. Let us ensure we are logged in to AWS via the terminal. If not logged in, run
aws configure
and enter the credentials as prompted. Let us make sure we have Docker running on our local machine.
Clone the Go repository
Before we clone the repo, let us have two separate terminal windows. One for the clone repo and the other one for the EC2 instance.
git clone https://github.com/Ademayowa/learn-d-compose.git
Run the commands below:
cd learn-d-compose
go mod tidy
cp .env.example .env && echo "POSTGRES_PASSWORD=mysecurepassword123" >> .env
Next, create an ECR repository by running the command below:
aws ecr create-repository --repository-name learn-d-compose --region us-east-1
Once we run the command above, a private repository registry called learn-d-compose is created on AWS ECR.
Build Docker image locally
docker-compose build
Copy the AWS account ID in the terminal and paste the value into the .env file with key AWS_ACCOUNT_ID:
aws sts get-caller-identity --query Account --output text
Authenticate Docker to ECR via the terminal and load the .env file into the current terminal session:
export $(cat .env | xargs)
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com
We should get a Login Succeeded message in the terminal.
Let us ensure we are using the correct AWS region where we have us-east-1.amazonaws.com and replace it based on our region.
Push the Docker image to ECR by running:
docker-compose push
Copy files from the local machine to the EC2 instance
Ensure the instance is running and SSH into the EC2 instance:
ssh -i ~/path_to_your_ssh_key.pem ubuntu@EC2_IP
Copy the .pem file and the docker-compose.yml file from the local machine to the EC2 instance by running:
scp -i ~/path_to_your_ssh_key.pem docker-compose.yml ubuntu@EC2_IP:~/
Next, copy the .env file by running:
scp -i ~/path_to_your_ssh_key.pem .env ubuntu@EC2_IP:~/
Installing packages on the EC2 instance
Now, let us open the terminal window where we have the EC2 instance running. Download the AWS CLI and run the command below:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Install unzip if it is not installed, and unzip the installer.
sudo apt install unzip -y
unzip awscliv2.zip
Run the installer and verify the installation
sudo ./aws/install
aws --version
Clean up the zip file by running:
rm -rf aws awscliv2.zip
Next, let us configure the AWS CLI with our credentials by running:
aws configure
We should get a login succeeded message in the EC2 instance terminal.
Log in to ECR on the EC2 instance by running:
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com
Next, update the system by running the command below:
sudo apt update -y && sudo apt upgrade -y
Installing Docker
Install Docker on EC2 by running:
sudo apt install docker.io -y
Start Docker services by running:
sudo systemctl start docker
sudo systemctl enable docker
Add Ubuntu user to Docker group on EC2 by running:
sudo usermod -aG docker $USER
Log out of the EC2 instance using "exit" in the terminal so the Docker group can take effect. Then SSH back into the instance using:
ssh -i ~/path_to_your_ssh_key.pem ubuntu@EC2_IP
Install Docker compose with the following command:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next, run:
sudo chmod +x /usr/local/bin/docker-compose
Verify Docker has been successfully installed on the EC2 instance by running:
docker --version
docker-compose --version
Verify files are present by running:
ls -la ~/
Pull the image from the ECR registry by running:
docker-compose pull
Start the services by running:
docker-compose up -d
Check if the containers are running with
docker-compose ps
Test the API endpoints if they are working fine:
curl -X POST http://localhost:8080/jobs \
-H "Content-Type: application/json" \
-d '{
"title": "Golang Backend Engineer",
"description": "API Integrations"
}'