Deploying Pocketbase with Docker, Nginx and SSL
Tags: pocketbase, docker, devops, webdev
Published: 2024-02-15 14:28:25

## Introduction

What is **Pocketbase**? [Pocketbase](https://pocketbase.io/) 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](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04), [nginx](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04) and [certbot](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04).

### Installing PocketBase With Docker

We'll use a slightly modified version of the docker-compose.yml from the [muchobien/pocketbase-docker](https://github.com/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:

```yaml
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:
 
```bash
$ 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:

```bash
$ 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:

```txt
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:

```bash
$ sudo nginx -t
```

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

```text
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
```

Then restart Nginx:

```bash
$ sudo systemctl restart nginx
```

Finally, we generate our SSL certificates using certbot:

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

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

- https://app.example.com - Serves the static content (html, css, images, etc.) from the `pb_public` directory.
- https://app.example.com/_/ - The admin dashboard UI
- https://app.example.com/api/ - The REST API


### Conclusion

Following this tutorial, you now have deployed PocketBase successfully.