DEV Community

Cover image for Deploying Pocketbase with Docker, Nginx and SSL
Russell 👨🏾‍💻
Russell 👨🏾‍💻

Posted on • Updated on

Deploying Pocketbase with Docker, Nginx and SSL

Introduction

What is Pocketbase? Pocketbase is an open-source backend solution offering a real-time database, file storage, and seamless user authentication with OAuth integration, all readily available right out of the box.

In this post, we’ll cover how to deploy Pocketbase using Docker and Nginx on an Ubuntu machine (although the instructions would also work for any Debian-based Linux distribution). Additionally, we will cover provisioning an SSL certificate using Certbot for enhanced security.

Prerequisites

Before we begin, ensure that you have the following:

  • An Ubuntu machine or server (these instructions apply to other Debian-based Linux distributions as well).
  • Docker installed on your system.
  • Basic knowledge of Docker and command-line usage.
  • Nginx installed on your system with certbot

Here are articles on installing docker, nginx and certbot.

Installing PocketBase With Docker

We'll use a slightly modified version of the docker-compose.yml from the muchobien/pocketbase-docker repository.

Step 1. Create a docker-compose.yml file in a directory of your choice. The contents of the file are provided below:

version: "3.7"
services:
  pocketbase:
    image: ghcr.io/muchobien/pocketbase:latest
    container_name: pocketbase
    restart: unless-stopped
    command:
      - --encryptionEnv 
      - ENCRYPTION 
    environment:
      ENCRYPTION: YOUR_ENCRYPTION_KEY 
    ports:
      - "8091:8090"
    volumes:
      - ./pb_data:/pb_data
      - ./pb_public:/pb_public 
      - ./pb_migrations:/pb_migrations 
    healthcheck: 
      test: wget --no-verbose --tries=1 --spider http://localhost:8090/api/health || exit 1
      interval: 5s
      timeout: 5s
      retries: 5
Enter fullscreen mode Exit fullscreen mode

Step 2. To start the docker container we run the following command in the terminal:

$ docker compose up -d --build
Enter fullscreen mode Exit fullscreen mode

This starts a pocketbase instance and exposes it on port 8091. The pb_data, pb_public, and pb_migrations folders created are bound to the folder in the container.

Step 3. Next, we create an Nginx instance to give access to our application. To keep things simple, we will work with the default configuration file. Run the command below:

$ nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Add the following code to the end of the file and save, replacing app.example.com with your domain name or the subdomain you want to create:

server {
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:8091;
        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;
    }

}
Enter fullscreen mode Exit fullscreen mode

Next, test that your Nginx configuration is valid:

$ sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

You should get a message like the one below if everything is fine:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Enter fullscreen mode Exit fullscreen mode

Then restart Nginx:

$ sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Finally, we generate our SSL certificates using certbot:

$ sudo certbot --nginx -d app.example.com
Enter fullscreen mode Exit fullscreen mode

Congrats, you have now set up PocketBase secured with SSL. The following are the routes:

Conclusion

Following this tutorial, you now have deployed PocketBase successfully.

Top comments (0)