How to Deploy PocketBase on a VPS Using Docker Compose
Deploying a robust backend for your applications can be challenging, but PocketBase simplifies this process with its lightweight and real-time capabilities.
In this guide, we’ll walk you through deploying PocketBase on a Virtual Private Server (VPS) using Docker Compose.
By the end of this tutorial, you’ll have a PocketBase instance running on your server.
Prerequisites
Before we begin, ensure you have the following:
- A VPS: Preferably running a Linux distribution like Ubuntu 22.04.
- Root Access: Ability to execute commands with
sudo
. - Domain Name: Pointed to your VPS’s IP address (e.g.,
pb.yourdomain.com
). - Basic Knowledge: Familiarity with the command line and Docker concepts.
Configuring PocketBase with Docker Compose
1. Create Project Directory
Organize your project files by creating a dedicated directory.
mkdir -p ~/projects/pocketbase
cd ~/projects/pocketbase
2. Initialize Docker Compose File
Create a docker-compose.yml
file to define your PocketBase service.
nano docker-compose.yml
Add the Following Content:
version: '3.8'
services:
pocketbase:
image: pocketbase/pocketbase:latest
container_name: pocketbase
restart: unless-stopped
ports:
- "127.0.0.1:8090:8080" # Accessible only via localhost
volumes:
- ./pb_data:/pb/pb_data
- ./pb_public:/pb/pb_public
- ./pb_migrations:/pb/pb_migrations
- ./pb_hooks:/pb/pb_hooks
command: ["serve", "--http=0.0.0.0:8080", "--dir=/pb/pb_data"]
Explanation:
- Image: Uses the official PocketBase Docker image.
- Ports: Maps container port
8080
to host port8090
bound tolocalhost
. This enhances security by restricting direct external access. - Volumes: Persists data and configurations by mapping host directories to container directories.
- Command: Starts PocketBase, specifying the data directory.
3. Create Necessary Directories
Ensure the directories for data, public assets, migrations, and hooks exist.
mkdir -p pb_data pb_public pb_migrations pb_hooks
Setting Up Nginx as a Reverse Proxy
To make PocketBase accessible via your domain with HTTPS, we’ll set up Nginx as a reverse proxy.
1. Configure Nginx
Create a new Nginx configuration file for PocketBase.
sudo nano /etc/nginx/sites-available/pb.yourdomain.com
Add the Following Configuration:
server {
listen 80;
server_name pb.yourdomain.com www.pb.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name pb.yourdomain.com www.pb.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/pb.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/pb.yourdomain.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# Security Headers
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
# Proxy Settings
location / {
proxy_pass http://127.0.0.1:8090;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
Replace pb.yourdomain.com
with Your Actual Domain.
2. Enable the Configuration
sudo ln -s /etc/nginx/sites-available/pb.yourdomain.com /etc/nginx/sites-enabled/
3. Test Nginx Configuration
sudo nginx -t
Expected Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4. Reload Nginx
sudo systemctl reload nginx
Securing with Let’s Encrypt SSL Certificates
Use Certbot to obtain and install SSL certificates for your domain.
1. Install Certbot
sudo apt install certbot python3-certbot-nginx -y
2. Obtain SSL Certificates
sudo certbot --nginx -d pb.yourdomain.com -d www.pb.yourdomain.com
Follow the Prompts:
- Email Address: Enter a valid email for urgent renewal and security notices.
- Agree to Terms: Yes.
- Redirect HTTP to HTTPS: Choose to redirect all traffic.
3. Verify SSL Installation
Access https://pb.yourdomain.com
in your browser. You should see a secure connection with a valid SSL certificate.
Final Verification
After completing the setup, perform the following checks to ensure everything is operational.
1. Access PocketBase API
curl http://localhost:8090/api/
Expected Response:
{
"code": 200,
"message": "PocketBase is running.",
"data": {}
}
2. Access Admin UI via Browser
Navigate to https://pb.yourdomain.com/_/
in your browser
Expected Outcome:
Access to the PocketBase Admin UI. like image below
Feel free to leave comments or reach out if you encounter any issues or have suggestions to improve this guide!