Hosting Pocketbase On Fly.io
Introduction
In the simplest of tech jargon...Pocketbase is the Open Source realtime backend in 1 file.
It is that tool which lets you create a production-ready backend in less than 5 minutes and lets you integrate nicely with your favorite frontend stack.
ProductHunt describes it as an open source Go backend that has embedded realtime database, files and users management, admin UI and REST-ish API, all compiled in 1 portable executable. You could think of it as a lightweight alternative to Firebase or Supabase.
Pocketbase saves data using SQLite, and it has many great object types for creating collections and supports real-time data!
In this post, you will be learning how to host a Pocketbase project on Fly.io, an infrastructure provider that has many dozens of server clusters around the world with out-of-the-box multi-region support. Fly.io provides seamless developer experience so that deploying a global workload really is an extension of local development.
Table Of Content
- Getting Started
- Creating Dockerfile
- Creating Docker Image
- Deploying Pocketbase Project to Fly.io
- Conclusion
Getting Started
First, you have to create a Dockerfile for Pocketbase.
To do so, open up a fresh terminal and create a new directory for your project and navigate into the directory. I called mine
my-pocketbase-docker
.
mkdir my-pocketbase-docker
cd my-pocketbase-docker
Creating Dockerfile
- Create a new empty file(Dockerfile) Add the following content to
Dockerfile
touch Dockerfile
- Add the following content to
Dockerfile
FROM alpine:latest
ARG PB_VERSION=0.10.1
RUN apk add --no-cache \
unzip \
# this is needed only if you want to use scp to copy later your pb_data locally
openssh
# download and unzip PocketBase
ADD https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zip /tmp/pb.zip
RUN unzip /tmp/pb.zip -d /pb/
EXPOSE 8080
# start PocketBase
CMD ["/pb/pocketbase", "serve", "--http=0.0.0.0:8080"]
Creating Docker Image
Having created the Dockerfile, create your Docker image by navigating to the folder containing the Dockerfile and running the command below
docker build -t pocketbase .
To test that your image works, run the command below.
docker run -it — rm -p 8080:8080 pocketbase
Up Next...Deploying to Fly.io
Deploying Pocketbase Project to Fly.io
- Install
flyctl
. You can find the commands to whichever operating system you use.
# Windows:
iwr https://fly.io/install.ps1 -useb | iex
# Linux
curl -L https://fly.io/install.sh | sh
# Mac
brew install flyctl
# or
curl -L https://fly.io/install.sh | sh
- You will be asked to Sign Up and you can do that using this command:
flyctl auth signup
The above command will open a browser window where you can register on the fly.io platform
After registration, login from your terminal using
flyctl auth login
Follow the installation instructions from https://fly.io/docs/hands-on/install-flyctl/.
Now that you have logged in we are going to proceed to deploy:
Navigate to the
my-pocketbase-docker
directory where you initially created the Dockerfile.Run
flyctl launch
and answer the follow-up questions like the name of your app, deployment region and so onWhen asked the last two questions below, reply with
n
? Would you like to set up a Postgresql database now?
? Would you like to deploy now?
A
fly.toml
configuration file will be created in the launch directory by theflyctl launch
command.Run
flyctl volumes create pb_data --size=1
to create and mount a 1GB free persistent volume.Select the same region you selected when you were asked your deployment region while running
flyctl launch
After this, add the below
mounts
config to thefly.toml
file
[mounts]
destination = "/pb/pb_data"
source = "pb_data"
- The
fly.toml
will now look like this
# fly.toml file generated for pocketbase-test on 2022-09-18T12:44:18+03:00
app = "pocketbase-test"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []
[experimental]
allowed_public_ports = []
auto_rollback = true
[mounts]
destination = "/pb/pb_data"
source = "pb_data"
# optional if you want to change the PocketBase version
[build.args]
PB_VERSION="0.10.1"
[[services]]
http_checks = []
internal_port = 8080
processes = ["app"]
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"
[[services.ports]]
force_https = true
handlers = ["http"]
port = 80
[[services.ports]]
handlers = ["tls", "http"]
port = 443
[[services.tcp_checks]]
grace_period = "1s"
interval = "15s"
restart_limit = 0
timeout = "2s"
Run
flyctl deploy
and you're good to go!To access your PocketBase dashboard navigate to
https://YOUR_APP_NAME.fly.dev/_/
To deploy new configuration/image changes, just run
flyctl deploy
again.
Conclusion
Pocketbase provides a lot of great features, especially when it comes to CRUD-based operations and could be used as a standalone app or as a Go framework/toolkit that enables you to build your own custom app specific business logic and still have a single portable executable at the end. One thing is for certain, it will come in handy for you as a developer and make life easier for you when coding.
Connect with me on Twitter if you have any questions.