Deploying Go App with NGINX + Docker to Amazon EC2
By: Bismo Baruno
Tags: Amazon Ec2, Deploy, Nginx, Go, Docker

Photo by pixabay.com

Hi everyone! With me again and again to share some experience, though or opinion about technology related with software engineering field. Today, I will share about DevOps things which is how to deploying your application to Amazon EC2. And we will use Docker + NGINX + Go programming language for this example.

Why AWS? AWS is a nowadays popular cloud platforms with many companies used it. You can see the detail in their official page https://aws.amazon.com/

And they provided some free tier service so we can easily learn their services. See https://aws.amazon.com/free/ for more information about this service.

Make sure you already have valid AWS account and go to this console link https://aws.amazon.com/console/

1. Setup EC2 Service

Choose EC2 Service from AWS Management Console page

Picture 1 Choose EC2 Service

Scroll down and find Launch Instance button. After you clicked it, you will be redirected to AMI list page.

Picture 2 Launch EC2 Instance

We will use Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type on Step 1 because it’s supported Free tier and have Docker repositories which is we will use container for deploying our app.

Picture 3 Choose AMI

Step 2 is for the Instance Type, you can pick any type, but we will choose t2 micro which is Free tier eligible. Next, click on Next: Configure Instance Details button.

Picture 4 Choose Instant Type

Then, if you want to have specific configuration, you can change based on your preferences. But, we will use the default configuration. Because we use the default, we can continue to Step 3 by click on Next: Add Storage button and in the Add Storage page, go to Step 4 by click Next: Add Tags button.

Picture 5 Configure Instance

Picture 6 Add Storage

Step 5 is Add Tags, we can define a tag for both instances and volumes. We will use Name for the key and go-app for the value.

Picture 7 Add Tags

On Step 6: Configure Security Group page, we will expose port 80 as a listening server and allow source from anywhere (0.0.0.0/0). Because we will use NGINX as a proxy and then redirected to our Go App.

Picture 8 Configure Security Group

Last step is Review. If everything seems good, click Launch button for creating your instance.

Picture 9 Review Instance Launch

If we prompted for a key pair, you can choose between create a new key pair or choose an existing key pair. Because we don’t have any key pair, we will create first.

Download Key Pair and save the private key file in a safe place because you’re not be able to download again after it created. Because you will need to connect the instance by SSH.

Then, Launch Instances button will be enable and ready to click. Let’s started by clicking the button.

Picture 10 Key Pair

After successfully launch the instance, you will redirected to Launch Status page. Click View Instances button to see the detail.

Picture 11 Launch Status

2. Connect to Instance

You can see the instance that we created already running with green status on My Instance page. In this page you can see the information for every instance that you have.

Picture 12 My Instance Page

Click Connect button to see how we connect to our instance. We will use SSH for connecting to instance.

Picture 13 How to Connect to Your Instance

Just copy the example command and paste in your terminal (I’m using Mac) and make sure your private key path is correct with file permission 400. Then, voila, we’re in!!

We also can use Pv4 Public IP instead of Public DNS (IPv4) to do SSH

Picture 14 Connected to Instance

3. Setup Docker

We will use Docker for this example. Why Docker? What is Docker?

TL;DR Docker is a set of platform as a service products that uses OS-level virtualization to deliver software in packages called containers, With this containers, you can easily setup your application to the server or migrating from one server to another server.

First, update the package using yum as package management with this command (-y means assume yes if prompted) :

sudo yum update -y

After completed, continue install Docker:

sudo yum install -y docker

Start docker service:

sudo service docker start

Make ec2-user to docker group to run command without sudo:

sudo usermod -a -G docker ec2-user

You would need to log out and log back in so that your group membership is re-evaluated.

4. Setup Images

Because we will try to keep clean our instance, then the option is we will do development our application on local, build the image and store to container registry. In this part, we will use Docker Hub (https://hub.docker.com/) because it’s free for public repositories.

If you considering for private repositories, you can paid on Docker Hub or using another service like Amazon ECR (https://aws.amazon.com/ecr/). Maybe we can try and I will create on another story.

Back to the application, we will need simple Go file and Dockerfile. Create main.go file with contain this code:

Dockerfile should contain this code:

Build the image with this command:

docker build -t momo89/go-hello-world .

Picture 15 Build Docker Image

Login to the Docker Hub (make sure you already have an account):

docker login

Push to the image to Docker Hub:

docker push momo89/go-hello-world:latest

Picture 16 Push Docker Image

5. Running Go App

After successfully created the Go App Docker image, we will try to pull and run it in our instance. The command is very simple:

docker run -p 3000:3000 -d momo89/go-hello-world

Picture 17 Pull Docker Image

Check the container service already running with:

docker ps

Picture 18 Check Container Service

Try to ping the app with curl command:

curl -s localhost:3000

Picture 19 Ping Go App

Yeah!! We have done with the Go App if you got the response from server

6. Running NGINX

Since NGINX image already exist in Docker Hub, we don’t need create the image. Just pull the image and load some config.

Create directory and some files for NGINX config

cd $HOME

mkdir nginx

cd nginx

touch Dockerfile

touch nginx.conf

Dockerfile for NGINX should be look like this:

And nginx.conf will be like this (we use 172.17.0.1 because it’s default Docker ip address):

Build the NGINX image:

docker build -t momo89/nginx .

Picture 20 Build NGINX Docker Image

Run NGINX docker with this command:

docker run -p 80:80 -d momo89/nginx

Picture 21 Run NGINX Docker Image

Check the NGINX service already running or not with:

docker ps

Picture 22 Check Service

Picture 23 Accessing Go App through HTTP

Voila, it’s works! Now you can access the Go App behind NGINX proxy!

Hope you enjoy it, I’m happy if this article useful for you! Happy Deploying!

Thank you!