MirageXをfly.ioで動かすまでのメモ
MirageXのサーバサイドをfly.ioで動かしたい。
git clone
したリポジトリで、fly launch
を実行する。
$ fly launch
Scanning source code
Detected a NodeJS app
Creating app in /home/kentaro/src/github.com/rheniumNV/mirage-x-template
We're about to launch your NodeJS app on Fly.io. Here's what you're getting:
Organization: Kentaro Kuribayashi (fly launch defaults to the personal org)
Name: mirage-x-template (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)
? Do you want to tweak these settings before proceeding? No
Created app 'mirage-x-template' in organization 'personal'
Admin URL: https://fly.io/apps/mirage-x-template
Hostname: mirage-x-template.fly.dev
installing: npm install @flydotio/dockerfile@latest --save-dev
added 15 packages, and audited 752 packages in 4s
107 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
create Dockerfile
Wrote config file fly.toml
Validating /home/kentaro/src/github.com/rheniumNV/mirage-x-template/fly.toml
Platform: machines
✓ Configuration is valid
If you need custom packages installed, or have problems with your deployment
build, you may need to edit the Dockerfile for app-specific changes. If you
need help, please post on https://community.fly.io.
Now: run 'fly deploy' to deploy your Node.js app.
Node.jsアプリであることが自動的に判定されて、必要な依存ライブラリや設定ファイルなどが追加される。
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: package-lock.json
modified: package.json
Untracked files:
(use "git add <file>..." to include in what will be committed)
.dockerignore
Dockerfile
fly.toml
no changes added to commit (use "git add" and/or "git commit -a")
環境変数を設定する。.env.sample
を.env
にコピーして、編集していく。
まず、fly.io上のURLを調べる。
$ fly status
App
Name = mirage-x-template
Owner = personal
Hostname = mirage-x-template.fly.dev
Image = -
Platform = machines
この情報に基づいて、.env
MIRAGEのURL関連の設定を以下の通り編集(ほかはそのまま)。
MIRAGE_URL="https://mirage-x-template.fly.dev/"
MIRAGE_PORT="80"
fly deploy
でデプロイする。
$ fly deploy
==> Verifying app config
Validating /home/kentaro/src/github.com/rheniumNV/mirage-x-template/fly.toml
Platform: machines
✓ Configuration is valid
--> Verified app config
==> Building image
Remote builder fly-builder-damp-glade-4066 ready
==> Building image with Docker
--> docker host: 20.10.12 linux x86_64
...
Visit your newly deployed app at https://mirage-x-template.fly.dev/
サーバが適切なアドレスをbindしてないという警告。実際、アクセスしてもつながらない。
WARNING The app is not listening on the expected address and will not be reachable by fly-proxy.
You can fix this by configuring your app to listen on the following addresses:
- 0.0.0.0:3000
0.0.0.0
でbindできるよう修正。
$ git diff src | cat
diff --git a/src/server/config.ts b/src/server/config.ts
index 3567fdb..f22b5a6 100644
--- a/src/server/config.ts
+++ b/src/server/config.ts
@@ -2,6 +2,7 @@ type Config = {
appCode: string;
mirage: {
port: string;
+ address: string;
serverId: string;
};
auth: {
@@ -9,12 +10,13 @@ type Config = {
};
};
-const { MIRAGE_PORT, MIRAGE_SERVER_ID, AUTH_URL, APP_CODE } = process.env;
+const { MIRAGE_PORT, MIRAGE_ADDRESS, MIRAGE_SERVER_ID, AUTH_URL, APP_CODE } = process.env;
export const config: Config = {
appCode: APP_CODE ?? "SampleMirageXApp",
mirage: {
port: MIRAGE_PORT ?? "3000",
+ address: MIRAGE_ADDRESS ?? "127.0.0.1",
serverId: MIRAGE_SERVER_ID ?? "xxx",
},
auth: {
diff --git a/src/server/index.ts b/src/server/index.ts
index 8889365..78f42fd 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -19,6 +19,7 @@ const insuring =
const app = express();
const server = http.createServer(app);
+server.listen(Number(config.mirage.port), config.mirage.address);
const wss = new WebSocketServer({ server });
app.use(express.json());
@@ -33,6 +34,7 @@ app.get(
const dummyConfig = {
mirage: {
port: config.mirage.port,
+ address: config.mirage.address,
serverId: config.mirage.serverId,
apiPath: {
info: "/info",
ふたたびfly deploy
したが、起動してこない。ログを見てみる。
$ fly logs -a mirage-x-template
...
2023-12-16T14:54:00Z app[32875161b04978] nrt [info]Error: Cannot find module 'dotenv/config'
...
dotenvがなくて起動できていない。
package.json
のスタートスクリプトはこうなっている。
"start": "node --require dotenv/config ./dist/server/index.js",
Dockerfile
を編集する。dev環境のnpmモジュールを削除している箇所を、とりあえずコメントアウトする。
# Remove development dependencies
# RUN npm prune --omit=dev
もう一度デプロイ。無事アクセスできた。
クライアントは以下のURLから入手できる。
これをResoniteの画面にD&Dすれば動くはずなのだが、「読み込み中」と表示されたまま先に進まない。
サーバのログを見てみると、なんか落ちてるぽい。
2023-12-16T16:18:31Z proxy[32875161b04978] nrt [info]Downscaling app mirage-x-template from 1 machines to 0 machines, stopping machine 32875161b04978 (region=nrt, process group=app)
2023-12-16T16:18:31Z app[32875161b04978] nrt [info] INFO Sending signal SIGINT to main child process w/ PID 306
2023-12-16T16:18:36Z app[32875161b04978] nrt [info] INFO Sending signal SIGTERM to main child process w/ PID 306
2023-12-16T16:18:41Z app[32875161b04978] nrt [warn]Virtual machine exited abruptly
勝手に落ちない設定があるようだ。
You can set auto_stop_machines = false under the [http_service] section of fly.toml to prevent fly-proxy from stopping the machines in case of inactivity.
https://community.fly.io/t/what-does-downscaling-app-mean/12309/3
false
にして再度デプロイ。
やっぱだめ。
というか、.env
が適用されていないようであった。
$ flyctl secrets list
NAME DIGEST CREATED AT
fly secrets import
を使って登録。
$ cat .env | fly secrets import
$ fly secrets list
NAME DIGEST CREATED AT
APP_CODE 7e94ed29b64b9ad4 1m19s ago
AUTH_URL cf8a374c31cd5c07 1m19s ago
CV_OWNER_ID 49bb8a67b557e235 1m19s ago
MIRAGE_ADDRESS aa5556239c8c9e6d 1m19s ago
MIRAGE_HOST_CV_PATH 49bb8a67b557e235 1m19s ago
MIRAGE_PORT 14ca8664195e9676 1m20s ago
MIRAGE_SERVER_ID 6c4c7a8ebe6a63cd 1m19s ago
MIRAGE_URL 29ae752b60342c97 1m19s ago
MIRAGE_USE_SSL_CV_PATH 49bb8a67b557e235 1m19s ago
PLATFORM_API_URL feab3b8e1c366af6 1m19s ago
PLATFORM_ASSETS_URL c7636f1dd77c32ad 1m19s ago
もう一度デプロイする。
クライアントのビルド時に環境変数がちゃんととれてないぽい。
しかたないので、 .dockerignore
で .env
を指定してるのをコメントアウト。
今度はいけた!
ビルド時にはfly secretsで登録した値は使えないらしい。
You can set secrets for your applications, but these are only available at run-time. They aren’t available when building your Docker image without a little extra work.
じゃあやっぱ.env
を.dockerignore
からはずすしかないか。
.env
をビルド時に含めることにしたので、上のほうでセットしたsecretsはunsetする。
$ fly secrets unset APP_CODE AUTH_URL CV_OWNER_ID MIRAGE_ADDRESS MIRAGE_HOST_CV_PATH MIRAGE_PORT MIRAGE_SERVER_ID MIRAGE_URL MIRAGE_USE_SSL_CV_PATH PLATFORM_API_URL PLATFORM_ASSETS_URL
Updating existing machines in 'mirage-x-template' with rolling strategy
-------
✔ [1/2] Machine 32875161b04978 [app] update succeeded
✔ [2/2] Machine 91852e33b45218 [app] update succeeded
-------
$ fly secrets list
NAME DIGEST CREATED AT