手軽にflask環境を構築したいときの備忘録として。
実行環境
- Mac OSX 10.15.4
- Docker version 19.03.8
ディレクトリ構成
test/
|- Dockerfile
|- docker-compose.yml
|- app/
|- app.py
各ファイルの記述
Dockerfile
- 以下の記事と同様に記述
- コンテナ内に
app
ディレクトリを作成するコマンドを追加- ローカルの
app
ディレクトリをマウントするため
- ローカルの
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install python3 python3-pip -y
RUN pip3 install flask
RUN mkdir /app
docker-compose.yml
このファイルで以下の手続きを定義。
-
build
: 指定したパスにあるDockerfileをもとにimage作成 -
command
: コンテナ起動後、コンテナ内で実行するコマンド -
volumes
: ローカルの./app
をコンテナ内の/app
にマウント -
ports
: ポート指定
version: '3'
services:
web:
build: .
command: python3 app/app.py
volumes:
- ./app:/app
ports:
- 5000:5000
app/app.py
- こちらも以下の記事と同様に記述
- Docker+Flaskによるお手軽Webアプリ開発 「Webアプリの準備」の項
以上で準備完了。
ビルド→コンテナ起動→Webサーバ起動
- 以下のコマンド一発!
$ docker-compose up -d
- コンテナが起動しているか確認
- Stateが「Up」ならOK
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------
test_web_1 python3 app/app.py Up 0.0.0.0:5000->5000/tcp
- http://localhost:5000/ にアクセスして「Hello world!!」が表示されれば構築成功!