Deploying Go Applications Using Docker on AWS


Docker is an open source ecosystem. that allows you to package applications into containers that are simple, lightweight, and portable; they will run in the same way regardless of which environment they run on.
It’s the list of the points that we will discuss:
- Setup EC2 instance.
- Connect to AWS EC2 via SSH.
- Install Docker & Golang on AWS.
- Write a simple Dockerfile to describe Go application.
- Run Docker containers on AWS.
1- Setup EC2 instance
Click on Services and click on EC2:
Then, click on “Launch Instance” :
Choose an AMI (Amazon Machine Image). We are going to choose “Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type” . The exact versions may change with time.
Choose an Instance Type: we will go with the lowest specs and cheapest configuration “t2.micro” which is enough for this tutorial:
Then click on “Configure Instance Details”. We left everything in next tabs as default so click on “Add Storage”.Next, click on “Add Tags” to assign a name to our instance Then click Click on “Configure Security Group“.


As you see we exposed the port number that need the server to listening on it. For this demo, I have made an application which is listening on port 3000, Therefore, I enabled HTTP port 3000 and I set the source column to ‘Anywhere 0.0.0.0/0’ .
Now, click on “Review And Launch” then “Launch“.
Create a new key pair: you can either choose an existing key pair or create a new key pair. Then click on Download Key Pair and save the private key file in a safe place. You’ll need to connect by SSH to your instance later.
When you’re ready, select “Launch Instances”:
Click the View your instances on the Instances page link and you will be redirected to your “My Instances” page, where you can monitor and configure your EC2 instance:
2- Connect to AWS EC2 via SSH:
Click on Connect button as below image and follow the instructions at the popup
3- Install Docker & golang on AWS:
Update the installed packages and package cache on your instance.
sudo yum update -y
Install the most recent Docker Community Edition package.
sudo yum install -y docker
Install golang
sudo yum install -y golang
Start the Docker service.
sudo service docker start
Add the ec2-user to the docker group so you can execute Docker commands without using sudo.
sudo usermod -a -G docker ec2-user
Then reboot the EC2 as below image
Verify that the ec2-user can run Docker commands without sudo.
docker info
Then add the following content in a file named “app.go” :
4- Write a simple Dockerfile to describe Go application:


5- Run Docker containers on AWS
Then run below command to run container from the image which we created.
docker run -d -p 3000:3000 go-app
Finally, if you click in browser our Public DNS:3000 you will that our app working
If you liked this post, please recommend it and share it with your followers.