Deploy dockerize Go app on AWS EC2

Published on 2023 - 10 - 20

Last month, I deploy my persional project on AWS EC2, It taks more than a week to work, I write down the process and share the process.

  1. Create AWS account
  2. DNS config
  3. ssh login
  4. install git and docker on EC2
  5. repos dir
  6. apps dir
  7. hooks for repos
  8. auto deploy
  9. Dockerfile for caddy, go, postgres
  10. store images
  11. store Postgres data
  12. git push product main
  13. source code on Github

Create AWS account

AWS provide one year free tire, You can reigister with your Credit Card, first year is free with limit, After one year , If your continue use the resource, it cost based on your services tie. AWS

DNS config

After you rigister AWS, you can build a VPS on AWS, and get a public IP, you can add A record on your domain name server. www.example.com to public IP

ssh login

When build a VPS, you can get a ssh key, download on your computer ,and put the key file on folder, And connect your server. ssh -i keyname.pem www.example.com, and add the key on your server.
ssh -i "keyname.pem" ec2-user@your-amazonawsname-1.compute.amazonaws.com

install docker, git on server

In Amazon Linux 2023, the default software package management tool is DNF.
dnf install git
dnf install docker

repos dir

put local repos on server repos

mkdir -p repos/example.git
cd repos/example.it
git init --bare

apps dir

build the app in this apps dir

mkdir -p apps/example.com
 git --work-tree=$HOME/apps/example.com \
> --git-dir=$HOME/repos/example.git \
> checkout -f main

git --work-tree=$HOME/apps/example.com --git-dir=$HOME/repos/example.git checkout -f main

hooks for repos

add post-receive in hooks

#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/main$ ]];
then
echo "Deploying main branch to production..."
cd ~/apps/example.com
git --work-tree=$HOME/apps/example.com \
    --git-dir=$HOME/repos/example.git \
    checkout -f main
echo "Restarting services..."
  docker-compose -f docker-compose.yml -f docker-compose.production.yml up --build --force-recreate -d
echo "Services restarted."
docker-compose -f docker-compose.yml -f docker-compose.production.yml  ls
docker-compose -f docker-compose.yml -f docker-compose.production.yml  ps
else
echo "Ref $ref successfully received, but only the main branch will be deployed"
fi
done

make it executable
chmod +x post-receive

auto deploy

on your local repos

git remote add product ec2-user@www.example.com:repos/example.git
git remote -v
 ```
## Dockerfile for caddy, go, postgres
### Caddy
Caddyfile
`www.example.com`
`reverse_proxy server:3000`


### Postgres
docker-compose.yml

version: '3.9'

services:
db:
image: postgres
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}

docker-compose.production.yml

version: '3.9'
services:
server:
build:
context: ./
dockerfile: Dockerfile
restart: always
volumes:

  - ~/data/example.com/images:/app/images

depends_on:


  - db

caddy:
image: caddy
restart: always
ports:

  - 80:80
  - 443:443

volumes:


  - ./Caddyfile:/etc/caddy/Caddyfile

db:
environment:
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}
volumes:

  - ~/data/guanyuchi.com/psql:/var/lib/postgresql/data/pgdata    
## store images 
mkdir to create dir to store images
`mkdir -p ~/data/guanyuchi.com/images`
## store Postgres data
mkdir to create dir to store Postgres Data
` mkdir -p ~/data/example.com/psql`

## git push product main

git push product main
```

Source code on Github

Aerial Camp