自宅に余っていた M1 Mac miniを再利用して好きにいじれるサーバー(今回はDocker runさせたサービスを公開する目的)を立てました。
手順
ブラウザでの作業
-
ドメインを購入
初年度100円くらいの格安ドメインを取得(以下oreore.meで説明)
※更新が高いので1年後にはドメインを買い直す予定です。 -
Cloudflareに登録
Cloudflare の無料プランに登録します。 -
Cloudflareを日本語化
ダッシュボード画面右上の Profile → Language でUIを日本語に変更します。 -
Cloudflareにドメイン登録
- 青いボタン「+ドメインのオンボード」を押す
- 既存のドメインを入力欄に
oreore.meと入力して「続行」 - DNSレコードを確認して「アクティベーション」
-
ドメインにSSL適用
- ダッシュボードで
oreore.meドメインをクリックし、ドメイン設定画面へ - 左メニューから SSL/TLS → 概要を選択
- 現在の暗号化モード:フレキシブル に設定(ローカル側がhttpなので)
- ダッシュボードで
Mac miniでの作業
- macにcloudflaredをインストール
$ brew install cloudflare/cloudflare/cloudflared
- cloudflaredにログインしてドメインを選択
$ cloudflared tunnel login
ブラウザでcloudflareにログイン画面が開くのでログインし、
トンネルで使うoreore.meドメインを選択
- トンネル作成 & トンネルIDをどこかにメモ
$ cloudflared tunnel create <トンネル名>
すると以下が表示がされるので、トンネルIDをメモ
c11---****-72a***e```の部分がトンネルID(実際は伏せ字なし)
Tunnel credentials written to /Users/<ユーザー名>/.cloudflared/c1*****1-****-****-****-72a********e.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.
Created tunnel <トンネル名> with id c1*****1-****-****-****-72a********e
- ~/.cloudflared/ にconfig.yml(トンネル設定ファイル)作成
tunnel: <トンネルID>
credentials-file: /Users/<ユーザー名>/.cloudflared/<トンネルID>.json
ingress:
- hostname: <サブドメイン1>.oreore.me
service: http://localhost:8080
- hostname: <サブドメイン2>.oreore.me
service: http://localhost:3456
- service: http_status:404
複数のdockerを公開したい場合は、上例のように複数 -hostname & service の行を書く
- Cloudflared を使って、各サブドメインをDNS設定する
Aレコードが〜とか気にしなくていいので楽ちん
cloudflared tunnel route dns itxtunnel <サブドメイン1>.oreore.me
cloudflared tunnel route dns itxtunnel <サブドメイン2>.oreore.me
- 公開したいdockerイメージをlocalhostで動かす
$ cd /my-docker-img/lamp(例)
$ docker-compose up -d
- トンネルを起動!(5秒くらいで開通)
$ cloudflared tunnel run <トンネルの名前>
- ブラウザからアクセスしてみる
https://<サブドメイン>.oreore.me/
感想
以前はMyDNSサービスを使って変動する自宅IPアドレスを定期報告する必要がありました。
ちょっとしたことでその更新が途切れたりしてたもんですが、この方法だとそれもなく便利です。