LoginSignup
6
1

More than 1 year has passed since last update.

GCEのdockerコンテナデプロイでFastAPIを動かす

Last updated at Posted at 2020-05-26

背景

メモリを結構使うジョブ用にGCE上にバッチサーバーを構築したい。
オーケストレーションはコンテナで管理したい。
GCEのコンテナデプロイって機能があるのでそれを使う。

VM と MIG へのコンテナのデプロイ

frameworkはなんとなくFastAPIを使ってみる。

Dockerイメージの作成

Dockerfileの用意

FROM python:3.8

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

RUN pip install -r requirements.txt

CMD uvicorn main:app --host 0.0.0.0 --port 80

EXPOSE 80

デプロイしたらそのままで動くようにした。
ホストは0.0.0.0にしないとアクセスできなくて悩んだりした。(詳細 -> docker上のアプリにlocalhostでアクセスしたらERR_EMPTY_RESPONSEが出る)

FastAPIのmain.pyの用意

FastAPIのREADMEのexampleを参考に。

main.py
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return "success"

requirements.txt

requirements.txt
click==7.1.2
fastapi==0.54.1
h11==0.9.0
httptools==0.1.1 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
pydantic==1.5.1
starlette==0.13.2
uvicorn==0.11.5
uvloop==0.14.0 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
websockets==8.

イメージをビルド


docker build -t batch_server:tag_name . 

イメージをContainer Registryにpush

Container Registryのクイックスタートを参考に。

docker tag batch_server:tag_name gcr.io/${YOUR_PROJECT}/batch_server:tag_name 
docker push gcr.io/${YOUR_PROJECT}/batch_server:tag_name

Container Registryのコンソール上にコンテナが表示されるはず。

コンテナ指定してデプロイする

Container Registryにイメージが上がったら、それを指定してGCEを起動すればいい。
コンソールからできる↓

スクリーンショット 2020-05-26 18.28.41.png

6
1
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
6
1