Deploying a FastAPI application doesn’t have to be complicated. With Docker and GitHub Actions, you can create a smooth, automated workflow that takes your app from code to production with minimal effort. In this guide, we’ll walk through how to containerize your FastAPI project, set up continuous deployment, and push updates directly to your VPS — all without manual intervention.
Check Docker Installation
If not installed:
1
2
| curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
|
Verify Docker installation:
Create SSH Key for GitHub actions to access VPS
1
| ssh-keygen -t ed25519 -C "github-actions-deploy" -f github-actions-vps-key
|
Copy public key
1
2
3
| cat github-actions-vps-key.pub
# Copy entire line
# Make sure that the path in a command matches .SSH keys file path
|
Add public key to the authorized keys
1
2
3
4
5
| # On VPS
nano ~/.ssh/authorized_keys
# Paste the content of github-actions-vps-key.pub
# Save and exit (Ctrl+O, Enter, Ctrl+X)
chmod 600 ~/.ssh/authorized_keys
|
Create GitHub Actions Deploy key and secrets
- Create new Deploy key in GitHub repository and add the value of public key generated on VPS.
- Create GitHub Actions secret (SSH_PRIVATE_KEY) and add the value of SSH key generated on VPS (file contents without .pub extension).
- Create GitHub Actions secret (VPS_HOST) and add the value of VPS IP.
- Create GitHub Actions secret (VPS_USER) and add the value of VPS user, root or any other user that is created.
Create GitHub Actiosns workflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| name: 🚀 Deploy FastAPI to VPS
on:
push:
branches:
- master # Change if your default branch is different
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 🧾 Checkout code
uses: actions/checkout@v4
- name: 📂 Copy project to VPS
uses: appleboy/scp-action@v0.1.7
with:
host: $
username: $
key: $
source: "."
target: "/home/ubuntu/my_project_folder"
- name: 🐳 Rebuild and restart Docker container
uses: appleboy/ssh-action@v0.1.10
with:
host: $
username: $
key: $
script: |
cd /home/ubuntu/my_project_folder
sudo docker stop my_project_folder || true
sudo docker rm my_project_folder || true
sudo docker build -t my_project_folder .
sudo docker run -d --name my_project_folder -p 8001:8001 my_project_folder
|
Create Docker file
Create Dockerfile in the project root to dockerize FastAPI project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| FROM python:3.11-slim
# 2️⃣ Set working directory inside the container
WORKDIR /app
# 3️⃣ Copy only requirements first for caching
COPY requirements.txt /app/
# 4️⃣ Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# 5️⃣ Copy the rest of your application code
COPY . /app
# 6️⃣ Expose the port your FastAPI app will run on
EXPOSE 8001
# 7️⃣ Command to run FastAPI via uvicorn on port 8001
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8001"]
|
Run GitHub Actions workflow
If GitHub Actions workflow (build) is successfull, on VPS, check if container is running:
To list all images, run:
To remove an image, run: