Skip to content

Instantly share code, notes, and snippets.

@seintun
Last active February 3, 2025 06:08
Show Gist options
  • Save seintun/4c0523c478012b3a99e5a536447ec1d4 to your computer and use it in GitHub Desktop.
Save seintun/4c0523c478012b3a99e5a536447ec1d4 to your computer and use it in GitHub Desktop.
Deploy Go 🐿 with Docker 🐳 & AWS

1. 🐳Install Docker on your Mac

  • Official site: https://docs.docker.com
  • Create a Docker Hub account
  • A Docker whale will appear on status bar and click to login with your username password

2. 🐳Create Dockerfile

  • cd inside your Go Project that has RESTful API
$ cd myproject && touch Dockerfile
// Paste the following snippets into Dockerfile
# Golang Dockerfile
FROM golang
ADD . /go/src/github.com/username/projectname
WORKDIR /go/src/github.com/username/projectname
RUN go get ./
RUN go build
ENTRYPOINT /go/bin/projectname
EXPOSE 8080

3. 🐳Build your Docker image

  • test by running on http://localhost (this guide will use 8080)
  • username is your Docker's username
🐿 
$ docker build -t projectname .
$ docker run -d -p 80:8080 projectname
🐿 
  • -d means run this detached, as a daemon, eg, not dependent on the terminal session
  • -p means map ports; mapping :

4. 🐳Create Docker tag to be pushed in Docker's Hub Repository

* docker tag <image ID>  <docker hub username>/<image name>:<version label or tag>
🐳
$ docker tag projectname username/projectname
$ docker login
$ docker push username/projectname
$ docker run -d -p 80:8080 username/projectname
// Run again at http://localhost:8080 but with docker being pulled from Docker Hub
🐳

5. 🐳Create/Login an AWS account at https://aws.amazon.com/console

  • Navigate to Services -> Compute -> EC2
  • Click Launch Instances and select "Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type" meant for Docker
    • t2.micro (Free tier eligible) is sufficient for starting up, press (Next: Configure Instance Details)
    • Press (Next: Add Storage) & (Next: Add Tags)
    • Press (Add another tag), insert "name" @ key & insert "ssh-and-http-from-anywhere" or projectname @ value, then Press (Next: Configure Security Group)
    • Press (Add Rule), pick HTTP from dropdown
    • Press (Review and Launch)
    • Press Launch
  • Select "Create a new key pair" & fill "Key pair name" with your projectname
  • Download Key Pair and store in secure place
  • Press (Launch Instances) and wait for it to set-up

6. 🐳Getting Connected with AWS

// Your key must not be publicly viewable for SSH to work
$ chmod 400 ~/.ssh/projectname.pem
// Press Connect on your AWS instance using its Public DNS after "ec2-ucer@""
$ ssh -i ~/.ssh/projectname.pem ec2-user@<public-DNS>
// Put 'yes' if the text below appear
The authenticity of host 'ec2-xx-xxx-x-xxx.us-west-1.compute.amazonaws.com (xx.xxx.x.xxx)' can't be established.
ECDSA key fingerprint is SHA256:GRwSxxxxxxxxxxJ2BMT/vVxxxxxE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-xx-xxx-x-xxx.us-west-1.compute.amazonaws.com (xx.xxx.x.xxx)' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
10 package(s) needed for security, out of 11 available
Run "sudo yum update" to apply all updates.

7. 🐳Environment set-up on your AWS machine

// Update & install docker
[ec2-user@ip-xxx-xx-x-xx ~]$ sudo yum update -y
[ec2-user@ip-xxx-xx-x-xx ~]$ sudo yum install -y docker
[ec2-user@ip-xxx-xx-x-xx ~]$ sudo service docker start
[ec2-user@ip-xxx-xx-x-xx ~]$ sudo usermod -a -G docker ec2-user
// Logout and log back in
🐳
[ec2-user@ip-xxx-xx-x-xx ~] exit
[ec2-user@ip-xxx-xx-x-xx ~]$ docker info
[ec2-user@ip-xxx-xx-x-xx ~]$ docker run -d -p 80:8080 username/projectname
[ec2-user@ip-xxx-xx-x-xx ~]$ docker images
[ec2-user@ip-xxx-xx-x-xx ~]$ docker ps
🐳

8. 🐳f you can see your projectname after running docker ps, then you can NOW visit your Public DNS (IPv4) from AWS instance

🐳Miscellaneous Docker's commands

// Check the docker version
$ docker version
// See all running containers on your machine
$ docker ps
// See all docker images on your machine
$ docker images
// Remove Docker-Whale images
$ docker rmi -f <image ID or image name>
  • -f is "with force"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment