🈯

PocketBase を Fly.io にデプロイ

2024/09/27に公開

PocketBaseFly.ioにデプロイします.

次のようなDockerfileを作成します.Going to productionにあるものと同じです.

Dockerfile
FROM alpine:latest

ARG PB_VERSION=0.22.21

RUN apk add --no-cache \
    unzip \
    ca-certificates

# 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/

# uncomment to copy the local pb_migrations dir into the image
# COPY ./pb_migrations /pb/pb_migrations

# uncomment to copy the local pb_hooks dir into the image
# COPY ./pb_hooks /pb/pb_hooks

EXPOSE 8080

# start PocketBase
CMD ["/pb/pocketbase", "serve", "--http=0.0.0.0:8080"]

Fly.ioを使ったことがなければ,Quickstart: Launch your appを参考にFly.io CLIのインストール,アカウント作成,Fly.io CLIからログインを行います.

Dockerfileがあるディレクトリでfly launch --build-onlyを実行してイメージをビルドします.

$ fly launch --build-only
Scanning source code
Detected a Dockerfile app
Creating app in xxxxxxxx
We're about to launch your app on Fly.io. Here's what you're getting:

Organization: xxxxxxxx             (fly launch defaults to the personal org)
Name:         xxxxxxxx                 (derived from your directory name)
Region:       Tokyo, Japan           (this is the fastest region for you)
App Machines: shared-cpu-1x, 1GB RAM (most apps need about 1GB of RAM)
Postgres:     <none>                 (not requested)
Redis:        <none>                 (not requested)
Tigris:       <none>                 (not requested)

? Do you want to tweak these settings before proceeding? Yes
failed opening browser. Copy the url (https://fly.io/cli/launch/xxxxxxxx) into a browser and continue
Waiting for launch data...⣽

設定を変更したいか聞かれるので,「Yes」を選択するとアプリケーションに割り当てるCPUやメモリを設定できます.今回はデフォルトのままで十分です.

PocketBaseはpb_dataディレクトリにデータを保存します.このディレクトリをFly.ioのVolumeを利用して永続化します.

まずVolumeを作成します.

fly volume create pb_data
$ fly volume create pb_data
Warning! Every volume is pinned to a specific physical host. You should create two or more volumes per application to avoid downtime. Learn more at https://fly.io/docs/volumes/overview/
? Do you still want to use the volumes feature? Yes
Some regions require a Launch plan or higher (bom, fra).
See https://fly.io/plans to set up a plan.

? Select region: Tokyo, Japan (nrt)
                  ID: vol_xxxxxxxx
                Name: pb_data
                 App: xxxxxxxx
              Region: nrt
                Zone: c914
             Size GB: 1
           Encrypted: true
          Created at: 27 Sep 24 14:00 UTC
  Snapshot retention: 5
 Scheduled snapshots: true

次にfly.tomlを開き,以下の設定を最後に追加します.

fly.toml
...(既存の設定)...
[mounts]
  destination = "/pb/pb_data"
  source = "pb_data"

以上で永続化の設定は完了です.

最後にfly deployを実行してアプリケーションをデプロイします.

$ fly deploy
==> Verifying app config
Validating xxxxxxxx
✓ Configuration is valid
--> Verified app config
==> Building image
==> Building image with Depot
--> build:  (​)
[+] Building 21.1s (9/9) FINISHED                                                                                                                 
 => [internal] load build definition from Dockerfile                                                                                         0.0s
 => => transferring dockerfile: 617B                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                             1.6s
 => [internal] load .dockerignore                                                                                                            0.0s
 => => transferring context: 2B

...(略)...

Visit your newly deployed app at https://xxxx.fly.dev/

デプロイが完了するとアプリケーションのURLが表示されます.https://xxxx.fly.dev/_/xxxxはアプリケーション名)にアクセスしたときにPocketBaseの管理用アカウント作成画面が表示されれば完了です.アプリケーションURLの最後に/_/が必要です.

Discussion