Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DEV Community

Cover image for How to Set Up GitHub Actions to Deploy a Simple Docker App on an EC2 Server
Adeniyi Olanrewaju
Adeniyi Olanrewaju

Posted on

How to Set Up GitHub Actions to Deploy a Simple Docker App on an EC2 Server

If you're working on a web app and want to automate your deployment from GitHub to your server, this article is for you. I’ll walk you through everything step by step—from creating an EC2 server on AWS to setting up GitHub Actions to deploy your Docker app.

🧱 What You Need

  • A GitHub repository with your app (Dockerized)

  • An AWS account

  • Basic knowledge of the command line

🖥️ Step 1: Launch an EC2 Instance on AWS

  1. Login to AWS Console → Go to EC2

  2. Click Launch Instance

  3. Choose a free-tier AMI, like Ubuntu 22.04 LTS

  4. Select t2.micro (free-tier)

  5. Create a new key pair (this will give you a .pem file to SSH later)

  6. Allow port 22 (SSH) in security group

  7. Launch the instance

🔑 Step 2: SSH into Your Server
Once your instance is running, SSH into the server from your local:

chmod 400 your-key.pem  # Make the key secure
ssh -i your-key.pem ubuntu@<your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode

You’re now inside your server 🎉

🐳 Step 3: Install Docker & Git
Run the following commands on the server:

sudo apt update
sudo apt install docker.io docker-compose git -y
sudo usermod -aG docker $USER
newgrp docker
Enter fullscreen mode Exit fullscreen mode

Make sure Docker works:

docker --version
Enter fullscreen mode Exit fullscreen mode

🗝️ Step 4: Setup SSH Key for GitHub Access
We’ll generate an SSH key to allow this server to pull from your GitHub repo:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Press Enter to save at the default location (/home/ubuntu/.ssh/id_rsa), and also press Enter when asked for a passphrase.

Then:

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Copy the output.

Go to GitHub > Settings > SSH and GPG Keys > New SSH key
Paste the public key there.

Now test:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

It should say Hi <your username!> You’ve successfully authenticated.

🐙 Step 5: Clone Your Repo

  • Visit your repo on github
  • Click the code button
  • Switch to the SSH tab
  • Copy the Git URL.
git clone git@github.com:your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode

Make sure your app is using Docker (e.g., you have a Dockerfile and docker-compose.yml).

🔐 Step 6: Add Your EC2 Private Key to GitHub Secrets
Go to the GitHub repository of your project:

  • Click Settings > Secrets and variables > Actions

  • Click New repository secret

    • Name: SSH_PRIVATE_KEY
    • Value: Paste the content of your .pem file (e.g., your-key.pem) Also add:
  • HOST → EC2 PUBLIC IP

  • USERNAME → ubuntu

⚙️ Step 7: Add GitHub Actions Workflow
In your project folder, create:

.github/workflows/deploy.yml
Enter fullscreen mode Exit fullscreen mode

Here’s the content:

name: Deploy App
run-name: Deploy - ${{ github.event.head_commit.message }}

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 5

    steps:
    - name: Deploy via SSH
      uses: appleboy/ssh-action@v0.1.10
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        port: 22
        script: |
          cd your-repo-folder
          git pull origin main
          docker compose up --build -d --force-recreate
          docker image prune -f
Enter fullscreen mode Exit fullscreen mode

🚀 Step 8: Push and Watch it Deploy
Every time you push to main, the workflow will:

  • SSH into the server

  • Pull the latest changes

  • Rebuild and restart your Docker app

You can view logs on GitHub under Actions tab.

Top comments (0)