10
3

More than 1 year has passed since last update.

DaisyUIでデコったPhoenixアプリをFly.ioにデプロイして楽しむ(2022年)

Last updated at Posted at 2022-05-08

TailwindDaisyUIが最近流行っていると聞き試してはみたものの、Fly.ioへのデプロイに手こずってしまい辛い思いをしました。今後は楽しくできるよう成功パターンをメモします。

PhoenixアプリをDaisyUIでデコる1方法やFly.ioについては日本語で丁寧に解説してくれている資料もありますので、ここでは細かいことを気にせずFly.ioへのデプロイ自体を楽しみたいと思います。

もし何か間違っている点があったり、もっとクールなやり方をご存知でしたらぜひコメント欄にお便りお願いいたします。

動作環境

elixir          1.13.4-otp-24
erlang          24.3.4
❯ mix phx.new --version
Phoenix installer v1.6.8
❯ fly version
fly v0.0.325 darwin/amd64 Commit: da2b638 BuildDate: 2022-04-28T04:00:48Z

Fly.ioへのサインアップ

初めての方はまずFly.ioへのサインアップが必要です。

サンプルアプリgit clone

サンプルアプリmnishiguchi/hello_phx_daisyui_flyioを作りましたのでそれをFly.ioへ楽しくデプロイします。

cd path/to/your/workspace

git clone https://github.com/mnishiguchi/hello_phx_daisyui_flyio

cd hello_phx_daisyui_flyio

サンプルアプリからデプロイ関係のファイルを削除

  • fly launchコマンドの実行中にDockerfileが検知されると必要なファイルが自動生成されないので、予めDockerfileとデプロイ関係のファイルを削除しておきます。
  • また過去のデプロイ時に使用した整合性の取れないファイルが残っているとエラーの原因になる可能性があります。
  • 次項でfly launchコマンドを打つと再生成されます。
rm -rf rel fly.toml Dockerfile

fly launchコマンドを打つ

  • ? App Name
    • 任意のアプリ名を入力してEnter、もしくは何もせずEnter
  • ? Select region
    • 任意の地域を指定してEnter、もしくは何もせずEnter
  • ? Would you like to setup a Postgresql database now?
    • Yes
  • ? Select configuration:
    • Development
  • ? Would you like to deploy now?
    • デプロイの前にDockerfileを修正したいのでNo
❯ fly launch
Creating app in /Users/mnishiguchi/src/hello_phx_flyio
Scanning source code
Detected a Phoenix app
? App Name (leave blank to use an auto-generated name): mnishiguchi-test-1
Automatically selected personal organization: Masatoshi Nishiguchi
? Select region: iad (Ashburn, Virginia (US))
Created app mnishiguchi-test-1 in organization personal
Set secrets on mnishiguchi-test-1: SECRET_KEY_BASE
Preparing system for Elixir builds
Installing application dependencies
Running Docker release generator
Wrote config file fly.toml
? Would you like to setup a Postgresql database now? Yes
For pricing information visit: https://fly.io/docs/about/pricing/#postgresql-clusters
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
Creating postgres cluster mnishiguchi-test-1-db in organization personal
Postgres cluster mnishiguchi-test-1-db created
  Username:    postgres
  Password:    1659337b1f06452fdb592b84db7d015bf41bcfa76d9a46de
  Hostname:    mnishiguchi-test-1-db.internal
  Proxy Port:  5432
  PG Port: 5433
Save your credentials in a secure place, you won't be able to see them again!

Monitoring Deployment

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

Connect to postgres
Any app within the personal organization can connect to postgres using the above credentials and the hostname "mnishiguchi-test-1-db.internal."
For example: postgres://postgres:1659337b1f06452fdb592b84db7d015bf41bcfa76d9a46de@mnishiguchi-test-1-db.internal:5432

See the postgres docs for more information on next steps, managing postgres, connecting from outside fly:  https://fly.io/docs/reference/postgres/

Postgres cluster mnishiguchi-test-1-db is now attached to mnishiguchi-test-1
The following secret was added to mnishiguchi-test-1:
  DATABASE_URL=postgres://mnishiguchi_test_1:V8uo66rOJJWUHMS@top2.nearest.of.mnishiguchi-test-1-db.internal:5432/mnishiguchi_test_1
Postgres cluster mnishiguchi-test-1-db is now attached to mnishiguchi-test-1
? Would you like to deploy now? No

Your Phoenix app should be ready for deployment!.

If you need something else, post on our community forum at https://community.fly.io.

When you're ready to deploy, use 'fly deploy --remote-only'.

本番のアプリの場合はちゃんと出力をコピーしておいたほうがよさそうです。練習だけなら多分無視して大丈夫。

Save your credentials in a secure place, you won't be able to see them again!

デプロイする前にDockerfileにNode.jsを追加

Note: The stand-alone Tailwind client bundles first-class tailwind packages within the precompiled executable. For third-party Tailwind plugin support, the node package must be used. See the tailwind nodejs installation instructions if you require third-party plugin support.

  # install build dependencies
  RUN apt-get update -y && apt-get install -y build-essential git \
+     && apt-get install -y curl \
+     && curl -sL https://deb.nodesource.com/setup_16.x | bash \
+     && apt-get install -y nodejs \
      && apt-get clean && rm -f /var/lib/apt/lists/*_*

  # prepare build dir
  	@@ -46,12 +49,19 @@ RUN mkdir config
  COPY config/config.exs config/${MIX_ENV}.exs config/
  RUN mix deps.compile

+ # install npm dependencies
+ COPY assets/package.json assets/package-lock.json ./assets/
+ RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
+
  COPY priv priv

  COPY lib lib

  COPY assets assets

+ # install NPM dependencies
+ RUN npm install --prefix assets
+
  # compile assets
  RUN mix assets.deploy

デプロイ

以下のコマンドを打つだけです。

fly deploy --remote-only

便利なflyコマンド

  • flyを打つと全コマンドが表示されます。
  • flyflyctlへのシンボリックリンクらしいです。2
  • とりあえず以下のコマンドを楽しみましょう。
fly
fly dashboard
fly open
fly status
fly logs

デプロイを削除

  • small applications for free」と書かれてはいますが念のために練習用のデプロイは遊んだ後にすぐ削除しておいた方が無難です。

CleanShot 2022-05-08 at 17.44.47.png

今日の一句

CleanShot 2022-05-08 at 15.39.01.gif

Elixirコミュニティに初めて接する方は下記がオススメです

Elixirコミュニティ の歩き方 -国内オンライン編-

https://speakerdeck.com/elijo/elixirkomiyunitei-falsebu-kifang-guo-nei-onrainbian

image.png

日本には28箇所のElixirコミュニティがあります

image.png

日程からイベントを探すならElixirイベントカレンダー:calendar:

** Elixirイベントカレンダー **

https://elixir-jp-calendar.fly.dev/

image.png

  1. 「デコる」という言葉はpiyopiyo.ex #7:感想投稿サイトをデコろう!(daisyUI、tailwind css)(2022/05/07)レポートから学びました。

  2. https://zenn.dev/koga1020/articles/b60725143bbadf6a2b5a#%E5%89%8D%E6%8F%90

10
3
1

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
10
3