DEV Community

Cover image for Deploy a Golang App to EC2 with Docker and AWS ECR
Mayowa Adeniyi
Mayowa Adeniyi

Posted on

Deploy a Golang App to EC2 with Docker and AWS ECR

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

In the clone repository terminal window run:

cd learn-d-compose
go mod tidy
cp .env.example .env && echo "POSTGRES_PASSWORD=mysecurepassword123" >> .env

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Once we run the command above, a private repository registry called learn-d-compose is created on AWS ECR.

ecr-repository

Build the Docker image locally

docker build -t learn-d-compose:latest .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Once we have pushed the Docker image to ECR, check the ECR repository on AWS and select learn-d-compose

uploaded-docker-image-ecr

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:~/
Enter fullscreen mode Exit fullscreen mode

copy-docker-compose-file

Next, copy the .env file by running:

scp -i ~/path_to_your_ssh_key.pem .env ubuntu@EC2_IP:~/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

We should have the two files as shown below

list-files-on-ec2

Next, update the system by running the command below:

sudo apt update -y && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Installing Docker

Install Docker on EC2 by running:

sudo apt install docker.io -y  
Enter fullscreen mode Exit fullscreen mode

Start Docker services by running:

sudo systemctl start docker

sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Add Ubuntu user to Docker group on EC2 by running:

 sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

install-docker-compose

Next, run:

sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Verify Docker has been successfully installed on the EC2 instance by running:

docker --version

docker-compose --version
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

download-aws-cli

Install unzip if it is not installed, and unzip the installer.

sudo apt install unzip -y

unzip awscliv2.zip
Enter fullscreen mode Exit fullscreen mode

Run the installer and verify the installation

sudo ./aws/install

aws --version
Enter fullscreen mode Exit fullscreen mode

verify-aws-installation-ec2

Clean up the zip file by running:

rm -rf aws awscliv2.zip
Enter fullscreen mode Exit fullscreen mode

Next, let us configure the AWS CLI with our credentials by running:

aws configure
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

output-aws-account-id

Deploying to EC2 with Docker Compose

Pull the Docker image from AWS ECR registry by running:

docker-compose pull
Enter fullscreen mode Exit fullscreen mode

Start the services by running:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Check if the containers are running with:

docker-compose ps
Enter fullscreen mode Exit fullscreen mode

docker-compose-up-command

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"
  }'
Enter fullscreen mode Exit fullscreen mode

Conclusion

This tutorial demonstrated how to deploy a Golang application to EC2 with Docker and AWS ECR.

Resources

Official AWS Documentation

Top comments (0)