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 🚀
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.
Go to your AWS Console → EC2 dashboard.
Click Launch Instance.
Choose an Amazon Linux 2023 AMI (default, free-tier eligible).
Select the t2.micro instance type (again, free-tier eligible).
Create a new key pair (PEM format), name it, and download it safely.
In the network settings, allow SSH, HTTP, and HTTPS from the internet.
Leave storage at default and click Launch Instance.
Boom! Your virtual server is now running.
Once your instance is live:
Click Connect → EC2 Instance Connect → Connect.
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.
Place all your Go files, Dockerfile, templates/ folder, and the PEM key in one folder.
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.
SSH into the instance if not already connected.
Run ls to confirm the files are transferred.
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.
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.
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.