LoginSignup
2
2

More than 1 year has passed since last update.

コンテナを使ってFlaskアプリをGCPにデプロイし、CI/CDパイプラインの構築もする

Last updated at Posted at 2021-10-17

この記事では、シンプルなFlaskアプリを使用し、DockerでコンテナライズしGoogle Cloud Platform上(Cloud Build、 Cloud Container Registry、Cloud Runを使用)にデプロイする方法をまとめています。GCPのCloud Buildを使用したCI/CDパイプラインの構築も一緒にまとめています。

 シンプルなFlaskアプリの作成

1. app.pyを作成
app.py
from flask import Flask, Response, jsonify, render_template, logging, request
app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)
2. templatesフォルダーを作り、その中にindex.htmlを作成
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>hello world!</h1>
</body>
</html>
3. Dockerfileを作成
FROM python:3.8

ENV PORT 80
ENV HOST 0.0.0.0

EXPOSE 80

RUN apt-get update -y && \
    apt-get install -y python3-pip

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app


ENTRYPOINT ["python", "app.py"]
4. pip freeze -r > requirements.txtを走らせ、requirements.txtを作成
requirements.txt
Flask==1.1.2
5. docker build .を走らせ、Dockerイメージを作成

NOTE: dockerコマンドを使用するためにはDockerをインストールしてください Dockerインストール

6. docker imagesを使い、IMAGE IDを取得する

2021101701.png

GCPとGitHubの設定

7. GCP上で新しいプロジェクトを作成する

2021101702.png

Project nameを記入 > Billing accountを選択 > CREATE

8. Github上で新しいレポジトリの作成をする

2021101703.png

Repository nameを記入 > Create repository

DockerイメージをGCP(Container Registory)上にアップロード

9. docker tag <imageid> gcr.io/<gcp-project-id>/<projectname>を走らせ、Dockerイメージにタグを追加

 <> 内は自分のプロジェクトに沿って変更

2021101704.png

docker imagesを走らせ、イメージにタグが付与されているか確認
2021101705.png

10. gcloud initを走らせ、正しいGCPプロジェクトにいるか確認

NOTE: gcloudを使用するためにはインストールが必要です
Cloud SDKインストール

11. gcloud auth configure-dockerを使い、Credentialを追加
12. GCP上で、Container Registry APIを有効化
13. docker push gcr.io/<gcp-project-id>/<projectname>を走らせ、DockerイメージをContainer Registry上にアップロード

2021101706.png

Container Registryのページに行き、イメージがアップロードされているか確認
2021101707.png

14. GCP上でCloud Build APIを有効化
15. Cloud Run上で, Create serviceページに行き以下を設定

2021101708.png

2021101709.png

Container imageを選択 (latestのもの) > Service nameを記入 > Regionを選択 (us-west1がおすすめ) > Authenticationのセクションで, Allow unauthenticated invocationsを選択> Create

16. Edit & Deploy new revisionのページを選択

2021101710.png

Containerタブ > Container portを80に変更 > Autoscalingのセクションで, maximumを1に変更 > Deploy
緑のアイコンが表示されたら, Cloud Run上にあるURLからウェブサイトにアクセス
以下のように表示されれば成功!

2021101713.png

CI/CDパイプラインの構築

17. cloudbuild.yaml を作成

(参照: https://cloud.google.com/build/docs/deploying-builds/deploy-cloud-run#building_and_deploying_a_container)
2021101711.png

Cloud Runのargs部分を写真黄色枠のように変更'--region', 'us-west1', '--platform', 'managed', '--port', '80'
 
オプション: index.html上のタイトルを変更 (CICD設定後の変化確認のため)

18. ファイルをGitHubにPUSH

git init

git remote add origin your-repo-url

git add .

git commit -m "first commit"

git push origin master

19. Cloud Build上で, トリガーの作成

Triggers タブ > Create Trigger > Nameを記入 > Source Repositry上で, 自分のGithubレポジトリを選択 > Configuration Type上で, Cloud Build configuration fileを選択 > Create

この設定により、GithubのレポジトリにPUSHが行われるたびにこのトリガーが走ります

オプション: RUNをクリック > Run Trigger > Historyタブをチェック > 赤いアイコンがでたら, Settingsタブ > Cloud run admin と service accounts を有効化 > Triggersに戻る > RUNを再び走らせる
2021101712.png

20. 緑アイコンになれば、URL上からアクセス可能に!
プロジェクトコード

https://github.com/risatoy/demo-flask-app

2
2
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
2
2