Deploying a containerized PocketBase instance to Fly.io
November 2, 2024
Requirements
I'd like to deploy a database for content and other data for this blog and various small projects. PocketBase is an ideal solution, being lightweight, quick to set up, and featuring an intuitive GUI that non-technical collaborators can comfortably use to contribute and update data.
Hosting
I'll deploy to fly.io, though the steps to containerize will make this a viable process for other VPS setups in the future.
1. Initialize directory
First:
...in the directory of your choosing. Then make the necessary files and folders for PocketBase and Docker:
In pb_data/.gitignore
we can tell git to ignore all the local data besides the .gitignore itself, so our folder is available but we don't version control the data:
2. Set up the Dockerfile
Create our Dockerfile. These are essentially instructions to be followed by Docker as it builds the image:
3. Create the package scripts
Our dev
script with run docker:start
if the container isn't built yet, or fallback to docker:run
if it's already there.
docker run
creates and starts a new container from a Docker image.
--name pb-starter
specifies the name of the container. In this case, it's named "pb-starter".
*-d
runs the container in detached mode (background). The container will continue running even after the terminal session ends.
-p 8090:8090
maps a container port to a host port.
8090
(left): The host port (your machine's port).8090
(right): The container port (the port exposed by the container).
All the -v
flags in docker:run
are creating volumes for the directories in our PocketBase project.
We can run:
Then:
Access the PocketBase UI locally at localhost:8090/_
!
We can log in and update the database to our liking. Migrations will be created and visible in our /pb_migrations
folder which we can version control in our repo. The data we create in /pb_data
will only exist locally.
4. Deployment - Fly.io CLI & login
We can deploy wherever Docker containers are supported. For fly.io we can follow these steps:
This installs the fly CLI.
This will authenticate our account through the browser.
5. Deployment - Configure project on Fly.io
This will allow us to set some preferences like location (PocketBase only scales vertically in a single location), and choose CPU and RAM amounts (1 CPU w 512mb will work for us to start). It will then attempt to build the container and should succeed.
6. Deployment - persisting data and getting online
This creates a 1gb pb_data
volume for our data. Adjust the http_sevice.concurrency
and [mounts]
portions of out fly.toml
file.
Finally...
And we're up and running .
7. CI/CD with Github Actions
Let's add a github action to deploy when we push changes to the repo. We need to add out FLY_API_TOKEN
to our actions repository secrets. We can generate the token:
Now, any migrations we generate by updating tables in PocketBase can be deployed when we push the main branch.
Helpful Sources
This workflow is credit to Johnny Magrippis, check out his tutorial here: Your own CMS + Backend in 15': How to deploy PocketBase to Fly.io 🚀 DevEx, version control, CI/CD! - YouTube