Amazon Elastic Compute (EC2) is a service by Amazon that allows the creation of virtual machines in the cloud.
Amazon Elastic Container Registry (ECR) is a cloud service by AWS that allows the storage of Docker images.
If most of our 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 Golang application to EC2 with Docker and AWS ECR.
Prerequisites
- Go 1.25+
- Gin
- Docker
- AWS Account
- You already have a running EC2 instance on AWS. Note that on the EC2 instance, the Amazon Machine Image (AMI) selected should be Ubuntu.
Getting Started
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.
Clone the Go repository
Before cloning the repository, we need to have two separate terminal windows. One for the clone repository and the other one for the EC2 instance.
Clone the repository by running:
git clone https://github.com/Ademayowa/learn-d-compose.git
In the clone repository terminal window run:
cd learn-d-compose
go mod tidy
cp .env.example .env && echo "POSTGRES_PASSWORD=mysecurepassword123" >> .env
An .env file is created. To get the value of the AWS_ACCOUNT_ID in the .env file, run:
aws sts get-caller-identity --query Account --output text
Copy the value output in the terminal (that's the AWS account ID) and paste it into the .env file.
Load the .env file into the current terminal session
export $(cat .env | xargs)
Log in to ECR 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
A "Login Succeeded" message is displayed in the terminal window.
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 the Docker image locally
docker build -t learn-d-compose:latest .
Tag the Docker image and push to ECR by running:
docker tag learn-d-compose:latest ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/learn-d-compose:latest
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/learn-d-compose:latest
Once we have pushed the Docker image to ECR, check the ECR repository on AWS and select learn-d-compose
Now that we have the Docker image, let us copy some files from our local machine to the EC2 instance.
Copy files from the local machine
Before copying files from the local machine, let us ensure the EC2 instance is running.
On the terminal window for the cloned repository, copy 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:~/
Now, let us check if the two files were successfully copied from our local machine to the EC2 instance.
Switch to the EC2 instance terminal window
SSH into the instance by running:
ssh -i ~/path_to_your_ssh_key.pem ubuntu@EC2_IP
ls -la
We should have the two files as shown below
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
Installing AWS CLI on EC2
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
Get the AWS_ACCOUNT_ID automatically in the terminal window and log in to ECR on the EC2 instance:
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
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
Deploying to EC2 with Docker Compose
Pull the Docker image from AWS 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 to check 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"
}'
Conclusion
This tutorial demonstrated how to deploy a Golang application to EC2 with Docker and AWS ECR.









Top comments (0)