How To Deploy Pocketbase Using Docker, Nginx and SSL

Written by russell11 | Published 2024/03/04
Tech Story Tags: devops | pocketbase | docker | webdev | nginx | oauth-integration | debian-based-linux | deploy-pocketbase-with-docker

TLDRPocketbase offers an open-source backend solution with real-time database, file storage, and OAuth integration. This guide walks you through deploying Pocketbase using Docker and Nginx on Ubuntu, ensuring secure access with SSL certificates generated via Certbot.via the TL;DR App

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

Step 2

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

$ docker compose up -d --build

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

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;
    }

}

Next, test that your Nginx configuration is valid:

$ sudo nginx -t

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

Then, restart Nginx:

$ sudo systemctl restart nginx

Finally, we generate our SSL certificates using certbot:

$ sudo certbot --nginx -d app.example.com

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.

Also published here.


Written by russell11 | I'm a software engineer who loves coding and making things run smoothly with DevOps. I enjoy using technology to create
Published by HackerNoon on 2024/03/04