Renderを使用するに至った経緯
Flaskでアプリ開発するにあたり、デプロイするサーバーを探していました。
そこで、無料で使用すること、クレジットカードを使用しなくていいことを条件に探した結果Renderが条件に合っていたので使用することにしました。
Renderでデプロイするまで
https://qiita.com/sykx_16g/items/40ae3015479fdfbfa10e
https://qiita.com/matsutogen/items/f29ad5c244fdca24e4cf
こちらの記事を参考に作業を進めました。
デプロイした結果webサイトが表示されない
上記のサイトを参考にGithub、Renderのアカウントを作成し、
ここまで進めることができたのですが、
webサイトのURLをクリックしてもページが表示されませんでした…
原因:そもそもGithubにデータが入ってなかった
解決のために:Renderを再プッシュ
以下のコマンドで、変更をプッシュしてRenderを再デプロイしました。
git add .
git commit -m "Fix deployment issues"
git push origin main
ここで数十分ほど時間がかかりました。
エラー発生
原因は、
Githubは、ファイルサイズが100MBを超えるファイルをプッシュできないそうです。
ですが、それより大きいファイルが含まれているために発生したようです。
Git LFS (Large File Storage) を使用する
GitHubで大きなファイルを管理するために、Git LFSを使う方法があるらしく、活用することにしました。
1,Git LFSの公式ページからインストールする
2,
git lfs install
を実行してインストールする。
3, 大きなファイルを Git LFSに追加する
例えば、model.ptという大きなファイルを追加する場合以下のコマンドでまず、ファイルの種類を設定します。
git lfs track "*.pt"
4, ファイルをGitに追加して、コミットする
git add .gitattributes
git commit -m "Add .gitattributes for LFS files"
このようにして
例えば、
git add model.pt にファイルを追加していきます。
5, 再デプロイしてみる
git commit -m "Add large files to Git LFS"
5,追加したファイルを再度追加してコミット
以下のコマンドで追加したファイルを再度追加してコミットします。
git add .gitattributes
git add <large-files>
git commit -m "Add large files to LFS"
git push origin main
そして、処理の実行を待ちます。。
またまた、失敗・・・
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
まだ、大きなファイルがGithubの制限を超えていることが原因のようです。
必要なファイルのみをリポジトリに保存する
私は、仮想環境内のファイルも含めていたため、仮想環境をリポジトリから除外します。
1,仮想環境を除外する
echo "仮想環境名" >> .gitignore
2,仮想環境のフォルダをキャッシュから削除します
git rm -r --cached 仮想環境名
3,変更をコミットする
git add .gitignore
git commit -m "Exclude virtual environment from repository"
no changes added to commit (use "git add" and/or "git commit -a")
コミットできませんでした。
仮想環境がGitで追跡されないようにする
1、以下のコマンドで仮想環境がGitで追跡されないようにする
echo "仮想環境名" >> .gitignore
git rm -r --cached 仮想環境名
2、他の変更もステージングに追加
.gitignore の変更や他のファイルも含めてコミットします。
git add .gitignore
git commit -m "Add .py39venv to .gitignore"
他の変更したファイルも同様に git add します。
3、リモートにプッシュする
git push origin main
エラー解決!
Renderで確認
ダッシュボードで
Manual Deploy → Deploy latest commit
を押してデプロイ終了まで待ちます。
またまたエラー1
ERROR: Could not find a version that satisfies the requirement torch==1.10.1 (from versions: 1.13.0, 1.13.1, 2.0.0, 2.0.1, 2.1.0, 2.1.1, 2.1.2, 2.2.0, 2.2.1, 2.2.2, 2.3.0, 2.3.1, 2.4.0, 2.4.1, 2.5.0, 2.5.1)
ERROR: No matching distribution found for torch==1.10.1
このようなエラーが出ました。
原因は、pythonのバージョンの指定をしていませんでした。
ここで、バージョンを直接指定しました。
またまたエラー2
==> Running 'gunicorn apps:app'
bash: line 1: gunicorn: command not found
==> Running 'gunicorn apps:app'
bash: line 1: gunicorn: command not found
お次は、このエラーメッセージが表示されました。
gunicornがインストールされてないようです。
pip install gunicorn
requirement.txtにも追加します。
pip freeze > requirements.txt
またまたエラー3
==> Running 'gunicorn apps.app:create_app('production')'
bash: -c: line 1: syntax error near unexpected token ('
bash: -c: line 1: gunicorn apps.app:create_app('production')'
==> Running 'gunicorn apps.app:create_app('production')'
bash: -c: line 1: syntax error near unexpected token ('
bash: -c: line 1: gunicorn apps.app:create_app('production')'
このようなエラーが表示されました。
gunicornコマンドの引数にあるシンタックスエラーのようです。
setting画面のstart comandで書き直しました。
またまたエラー4
app.config.from_object(config[config_key])
KeyError: 'production'
今度はこのエラーです。
configの中でproductionを設定していないのが、原因のようでした。
これを追加したところ、、、
完了!!
Liveになっていれば成功のようです。
webページに無事アクセスできました。
結論
初めてのデプロイ、難しい・・・
詳しく理解できていない所もあったので、理解に努めます。。
エラー対処の記録でしたが、力になれますと幸いです!