Production PocketBase Dockerfile & Docker Compose Setup with Persistent pb_data Volumes
Deploy PocketBase in production with Docker Compose and Alpine Linux. Persist SQLite pb_data volumes, auto-apply pb_migrations and pb_hooks on boot, and stream real-time backups with Litestream.
PocketBase embeds an ultra-fast SQLite database (in WAL mode), real-time SSE subscriptions, auth, and file storage inside a single static Go binary consuming less than 35 MB of idle RAM. When containerizing PocketBase with Docker Compose, you must (1) bind PocketBase to `--http=0.0.0.0:8090` inside the container while publishing only `127.0.0.1:8090:8090` (or routing via Traefik/Nginx) on the host, (2) mount a persistent named volume to `/pb/pb_data` so recreating the container never wipes `data.db` or uploaded files, and (3) copy `pb_migrations` and `pb_hooks` into the image so schema updates apply automatically on container startup.
Verified Root Causes in Production Environments
- Ephemeral Container Filesystem Data Loss: Deploying PocketBase without mounting `/pb/pb_data` to a persistent Docker volume or host bind mount deletes your entire SQLite database (`data.db` and `auxiliary.db`) on the next `docker compose up -d --build`.
- Binding to 127.0.0.1 Inside the Dockerfile: Running `./pocketbase serve` without `--http=0.0.0.0:8090` binds to container loopback (`127.0.0.1:8090`), causing 502 Bad Gateway errors from Traefik or Nginx.
- Unencrypted App Settings in Database: Omitting `--encryptionEnv=PB_ENCRYPTION_KEY` leaves SMTP passwords and S3 storage credentials stored as plain text inside `_params`.
1. Production Multi-Arch PocketBase Dockerfile With pb_migrations & pb_hooks
Because PocketBase does not publish an official pre-built image on Docker Hub (to encourage developers to bake in their own `pb_migrations/`, `pb_hooks/`, or custom Go extensions), building a minimal 25 MB `alpine:3.20` Dockerfile is the recommended production pattern.
By copying your local `pb_migrations` directory into `/pb/pb_migrations` inside the image, PocketBase automatically executes any unapplied JavaScript migration files the instant the container starts—giving you reproducible, zero-click schema deployments across staging and production.
FROM alpine:3.20
ARG PB_VERSION=0.22.22
ARG TARGETARCH=amd64
RUN apk add --no-cache \
unzip \
ca-certificates \
curl \
tzdata
WORKDIR /pb
# Download the official PocketBase static Go binary for the target architecture
RUN curl -fsSL -o pb.zip \
"https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_${TARGETARCH}.zip" \
&& unzip pb.zip -d /pb/ \
&& rm pb.zip \
&& chmod +x /pb/pocketbase
# Copy version-controlled JS schema migrations and server-side hooks
COPY ./pb_migrations /pb/pb_migrations
COPY ./pb_hooks /pb/pb_hooks
EXPOSE 8090
# Bind to 0.0.0.0:8090 inside container and encrypt sensitive settings with PB_ENCRYPTION_KEY
CMD ["/pb/pocketbase", "serve", "--http=0.0.0.0:8090", "--dir=/pb/pb_data", "--encryptionEnv=PB_ENCRYPTION_KEY"]2. Docker Compose Stack With Persistent pb_data Volume & Traefik Labels
In your `docker-compose.yml`, mount a named volume (`pb_data:/pb/pb_data`) to persist your SQLite databases (`data.db`, `data.db-wal`, `data.db-shm`) and local file storage (`/pb/pb_data/storage`).
Note that `PB_ENCRYPTION_KEY` must be an exact 32-character random string (generate via `openssl rand -hex 16`), which PocketBase uses to AES-encrypt your S3 and SMTP credentials inside the database.
services:
pocketbase:
build:
context: .
dockerfile: Dockerfile
container_name: vexlo-pocketbase
restart: unless-stopped
environment:
# Must be exactly 32 characters (e.g., openssl rand -hex 16)
PB_ENCRYPTION_KEY: ${PB_ENCRYPTION_KEY}
TZ: Asia/Kolkata
# Bind to 127.0.0.1 if using host Nginx, or omit ports when using Traefik proxy network
ports:
- "127.0.0.1:8090:8090"
volumes:
- pb_data:/pb/pb_data
networks:
- proxy
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.pb.rule=Host(`pb.vexlo.cloud`)"
- "traefik.http.routers.pb.entrypoints=websecure"
- "traefik.http.routers.pb.tls.certresolver=letsencrypt"
- "traefik.http.services.pb.loadbalancer.server.port=8090"
healthcheck:
test: ["CMD", "curl", "-f", "http://127.0.0.1:8090/api/health"]
interval: 15s
timeout: 5s
retries: 3
volumes:
pb_data:
networks:
proxy:
external: true3. Configuring Nginx SSE Streaming & Automated Superuser Creation
PocketBase uses Server-Sent Events (SSE) on `/api/realtime` for live database subscriptions. If you proxy PocketBase through Nginx, disable `proxy_buffering off;` so real-time events stream immediately to clients without buffering delays. To create your initial production admin superuser without opening the web installer, execute `./pocketbase superuser upsert` inside the running container.
When scaling PocketBase behind Cloudflare or Traefik on Vexlo Cloud, configure PocketBase's built-in S3 storage adapter (Settings -> Files storage) to point to Cloudflare R2 or a local MinIO container so large user file uploads do not bloat your local SQLite backup archives. PocketBase signs S3 file URLs natively and streams uploads directly without buffering entire files in container RAM.
# Create or update the production PocketBase superuser via CLI
docker exec -it vexlo-pocketbase \
/pb/pocketbase superuser upsert [email protected] "YourStrongPassword2026!" --dir=/pb/pb_dataPre-Deployment Verification Checklist
- Pass `--http=0.0.0.0:8090` to `/pb/pocketbase serve` in the Dockerfile `CMD`.
- Verify `/pb/pb_data` is backed by a named Docker volume (`pb_data:/pb/pb_data`) before writing production data.
- Set a 32-character `PB_ENCRYPTION_KEY` environment variable and pass `--encryptionEnv=PB_ENCRYPTION_KEY`.
- Disable `proxy_buffering off;` in Nginx or configure Cloudflare/Traefik timeouts so `/api/realtime` SSE connections stay alive.
Deploy on Vexlo Cloud in 60s (₹149/mo UPI)
Automated Docker & Nixpacks builds, managed PostgreSQL/Redis, and AI Deployment Doctor. Instant INR UPI for India or USD/EUR/GBP across 17 global currencies.
Frequently Asked Questions
Why did all my PocketBase collections and users disappear after redeploying my Docker container?
If `/pb/pb_data` is not mounted to a persistent Docker volume or host folder, PocketBase writes `data.db` inside the ephemeral container writable layer, which is destroyed whenever the container is rebuilt or recreated.
How much RAM and CPU does a PocketBase Docker container need?
Because PocketBase is compiled in Go and uses embedded SQLite in WAL mode, it idles at 15–30 MB of RAM and can easily serve 10,000+ concurrent real-time connections on a single ₹149/month Vexlo Cloud container or 1 vCPU VPS.
How do I back up a running PocketBase SQLite database safely?
Either trigger PocketBase's built-in hot backup API (`Settings -> Backups` in the UI, which supports S3/Cloudflare R2 upload via `VACUUM INTO`) or run a Litestream sidecar container that continuously replicates WAL frames to S3/R2.