Deploying a Go Web Server to AWS EC2 with Docker

Learn how to deploy a Go-based web server using Docker on an AWS EC2 t2.micro instance. A beginner-friendly walkthrough covering code setup, EC2 provisioning, Docker installation, and live server deployment.

Stuti Gupta

3 months ago

deploying-a-go-web-server-to-aws-ec2-with-docker

If you’ve ever wanted to deploy a Go web server on AWS using Docker but didn’t know where to start, this guide is your shortcut. We'll walk through every step—from writing a basic Go server to containerizing it and getting it live on an EC2 instance (for free, if you use the t2.micro tier).

Let’s go 🚀

Step 1: Understand the Go Web Server Code & Dockerfile

Here’s the minimal Go server we’ll deploy. It listens on port 8080 and renders a static HTML page from the templates folder.

package main

import (

"fmt"

"log"

"net/http"

"text/template"

)

func main() {

fmt.Println("Starting server on port 8080")

http.HandleFunc("/", handler)

log.Fatal(http.ListenAndServe(":8080", nil))

}

func handler(w http.ResponseWriter, r *http.Request) {

tmpl := template.Must(template.ParseFiles("templates/index.html"))

tmpl.Execute(w, nil)

}

Here’s the Dockerfile to containerize the app:

FROM golang:1.22.5-alpine

WORKDIR /app

COPY go.* ./

RUN go mod tidy

COPY . .

COPY templates ./templates

RUN go build -o app

EXPOSE 8080

CMD ["./app"]

We’re using Alpine for a minimal image and exposing port 8080. Once built, this image will run your Go app as a container.

Step 2: Launch an AWS EC2 t2.micro Instance

  1. Go to your AWS Console → EC2 dashboard.

  2. Click Launch Instance.

  3. Choose an Amazon Linux 2023 AMI (default, free-tier eligible).

  4. Select the t2.micro instance type (again, free-tier eligible).

  5. Create a new key pair (PEM format), name it, and download it safely.

  6. In the network settings, allow SSH, HTTP, and HTTPS from the internet.

  7. Leave storage at default and click Launch Instance.

Boom! Your virtual server is now running.

Step 3: Install Docker & Dependencies on the EC2 Instance

Once your instance is live:

  1. Click Connect → EC2 Instance ConnectConnect.

  2. A terminal window will appear—perfect.

Run the following commands to install Docker:

sudo dnf update -y

sudo dnf install docker -y

sudo systemctl start docker

You now have Docker up and running on your EC2 box.

Step 4: Copy Code & Run Docker Container on EC2

On Your Local Machine

  1. Place all your Go files, Dockerfile, templates/ folder, and the PEM key in one folder.

  2. Secure the key with:

chmod 600 demo-key-pair.pem

Transfer everything to your EC2 instance:

scp -i demo-key-pair.pem -r * ec2-user@<EC2-PUBLIC-IP>:/home/ec2-user

🔁 Replace <EC2-PUBLIC-IP> with your actual instance’s public IP.

On Your EC2 Instance

  1. SSH into the instance if not already connected.

  2. Run ls to confirm the files are transferred.

  3. Build the Docker image:

sudo docker build . -t go-docker-ec2:latest

Run the container:

sudo docker run -p 80:8080 go-docker-ec2:latest

This binds container port 8080 to EC2's port 80—so your Go server is now publicly accessible.

Final Result 🎉

Go to your EC2 instance’s public IP in a browser (just type http://<EC2-PUBLIC-IP>), and you should see your simple HTML page being served from inside a Docker container on AWS.

Troubleshooting Tips

  • If nothing shows up in the browser, ensure port 80 is allowed in security group settings.

  • Run docker ps to verify your container is running.

  • Use docker logs <container_id> to debug Go server output if needed.

Thanks for reading!
Got stuck somewhere? Drop a comment, and I’ll help you out. 🙌

Credits:

  • Go image: golang:1.22.5-alpine

  • Cover photo: Unsplash / Pexels (use any free-use image here)

  • Written at Roboto Studio—where code meets caffeine.