LoginSignup
4
4

More than 1 year has passed since last update.

Docker環境下でのfly.ioのデプロイ

Last updated at Posted at 2023-03-20

はじめに

初めてfly.ioにデプロイしたので、メモも兼ねてその方法を今回は書いていこうかなと思います。
とは言っても、公式ドキュメントを参考にしているだけなので、目新しいことは書いていないですけど、少しでも参考になれば幸いです。

環境

・macOS Ventura 13.2.1
・Docker version 20.10.22
・Ruby 3.1.2
・Rails 7.0.4
・iterm2

今回はこちらで環境構築したものをデプロイしていきます。

手順

こちらをもとに進めていきます。
以下をターミナルで叩きながら実施していきます。

flyctlのインストール実施

brew install flyctl

サインアップ

・以下を叩くと、自動でブラウザに登録ページにアクセスするので、メアドかgithubアカウントで登録してください。

flyctl auth signup

サインイン

・以下を叩くと、自動でサインインできます。

fly auth login

RailsとPostgresのサーバーの起動

fly launchを叩くと、以下が表示されます

※ここにないのですが、進める中でクレジットカード情報を登録していない方は、以下のメッセージが出るので登録してください。

Error We need your payment information to continue! Add a credit card or buy credit: https://fly.io/dashboard/your-name/billing

それでは本題へ。再度fly launchを叩くと、以下が表示されます。

hoge@MacBook-Air-2 sample-app % fly launch
Creating app in /Users/hoge/documents/sample-app
Scanning source code
Detected a Rails app

# アプリケーションの名前を指定します。ここは自分がつけたい名前を記入します。
? Choose an app name (leave blank to generate one): sample-app

#東京リージョンを指定。いくつか選択肢でると思うので、矢印キーで選びます。
? Choose a region for deployment: Tokyo, Japan (nrt)

#PostgreSQLを作成する
? Would you like to set up a Postgresql database now? Yes

#無料枠を使用するので、Development〜を選択します。
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
Creating postgres cluster in organization personal
Creating app...
Setting secrets on app sample-app-db...
Provisioning 1 of 1 machines with image flyio/postgres-flex:15.2@sha256:8e00d751bb9811bc8511d7129db2cc5a515449cf4a7def8f1d60faacb2be91c6
Waiting for machine to start...
Machine 5683d3d0b6478e is created
==> Monitoring health checks
  Waiting for 5683d3d0b6478e to become healthy (started, 3/3)

#作成されたPostgresの情報はこのタイミングで表示されるため一応メモしといてください
Postgres cluster sample-app-db created
  Username:    postgres
  Password:    xxxxxxxxxxxxxx
  Hostname:    sample-app-db.internal
  Flycast:     xxxxxxxxxxxxx
  Proxy port:  5432
  Postgres port:  5433
  Connection string: postgres://postgres:xxxxxxxxxxxxxxxx@sample-app-db.flycast:5432

省略

#Redisを使用するか選択。今回は使用しないので、Nと入力
? Would you like to set up an Upstash Redis database now? No

#dockerignoreを作成するか選択。Nと入力。
? Create .dockerignore from 1 .gitignore files? No
Wrote config file fly.toml

Once ready: run 'fly deploy' to deploy your Rails app.

これで、GemfileGemfile.lockfly.tomlが作成・変更されると思います。fly.tomlは特に内容を変更せずともデプロイできたので一旦触らなくてもいいかと思います。

デプロイ

fly deployをする前に

デプロイコマンドを叩いていくのですが、fly openすると、ブラウザに
Blocked host: sample-app.fly.devというエラーが出るかもしれません。
これは、DNSリバインディング攻撃を防止する ActionDispatch::HostAuthorization middleware によるものらしく、Rails6から追加されています。
参考記事
なので、config/environments/development.rbに以下を追加します。

config.hosts << "sample-app.fly.dev"

これで、ブラウザが開くと思います。

fly deploy

そしたら、デプロイコマンドを実行します。するとbuildすると思うので、deployed successfullyと出たら成功です。私の場合、ここでエラーが出ることはありませんでした。

hoge@MacBook-Air-2 sample-app % fly deploy
Update available 0.0.485 -> 0.0.487.
Run "fly version update" to upgrade.
==> Verifying app config
--> Verified app config
==> Building image
Remote builder fly-builder-holy-wildflower-4552 ready
==> Creating build context
--> Creating build context done
==> Building image with Docker
--> docker host: 20.10.12 linux x86_64
[+] Building 5.7s (0/1)
[+] Building 103.5s (14/14) FINISHED
 => [internal] load remote build context                                                                         0.0s
省略

 1 desired, 1 placed, 1 healthy, 0 unhealthy [health checks: 1 total, 1 passing]
--> v0 deployed successfully

fly open

fly.openを叩き、デプロイしたアプリケーションを開きます。

デプロイ後の進め方

開発環境で実装を追加したら、fly deployを叩いてから、fly openとするとデプロイ先にも追加した機能が反映すると思います。

終わりに

fly.ioは日本語で参考記事が少なく、デプロイするのも一苦労すると思います。
公式ドキュメントや英語の記事を参考にして、僕が間違った解釈をしていることもあると思うので、何かあればコメントでご教授いただきたいです。
少しでも参考になればありがたいです。ありがとうございました。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4