3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

5分でFlaskをDockerで起動させる

Last updated at Posted at 2022-08-02

環境

  • Windows 10
  • WSL2 + Docker
  • Ubuntu
  • Python 3

ディレクトリ構成

flask
├── Dockerfile
├── docker-compose.yml
├── hello.py

各種ファイル

今回は最小構成にします。

Dockerfile

FROM python:3
ENV PTYHONUNBUFFERED 1
WORKDIR /app
RUN pip install flask

docker-compose.yml

environmentでアプリケーションが動作するコントローラー的なファイルをFLASK_APP=hello.pyに設置しないとError: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
といったエラーが発生する。ローカルで起動するポートを3000に指定しておく。

version: '3.3'
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: flask
    volumes:
      - ./:/app/
    ports:
      - "3000:3000"
    command: flask run --host 0.0.0.0 --port 3000
    environment:
      - FLASK_APP=hello.py

hello.py

TOPでHello World出力させるシンプルなものを用意。

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello World!</h1>'

## 起動用
if __name__ == "__main__":
    app.run(debug=True)

サーバ構築+起動

Build

$ docker-compose build
Building api
Step 1/4 : FROM python:3
 ---> xxxxxxxxxxx
Step 2/4 : ENV PTYHONUNBUFFERED 1
 ---> Running in xxxxxxxxxxx
Removing intermediate container xxxxxxxxxxx
 ---> xxxxxxxxxxx
Step 3/4 : WORKDIR /app
 ---> Running in xxxxxxxxxxx
Removing intermediate container xxxxxxxxxxx
 ---> xxxxxxxxxxx
Step 4/4 : RUN pip install flask
 ---> Running in xxxxxxxxxxx
Collecting flask
省略
Successfully built xxxxxxxxxxx
Successfully tagged flask_api:latest

起動

$ docker-compose up
Recreating flask ... done
Attaching to flask
flask  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
flask  |  * Serving Flask app 'hello.py'
flask  |  * Debug mode: off
flask  |  * Running on all addresses (0.0.0.0)
flask  |    WARNING: This is a development server. Do not use it in a production deployment.
flask  |  * Running on http://127.0.0.1:3000
flask  |  * Running on http://172.19.0.2:3000 (Press CTRL+C to quit)
flask  | 172.19.0.1 - - [02/Aug/2022 13:33:55] "GET / HTTP/1.1" 200 -

確認

$ curl http://localhost:3000
<h1>Hello World!</h1>

もしくはブラウザでhttp://localhost:3000を入力

以上!!!

テンプレートを使いたい

どうせなら、pyファイルとWebコンテンツ内容を切り離し、別個にhtmlテンプレートファイルを用意したい。

ディレクトリ構成

flask
├─ templates
    └─ index.html (new!!)
├─ Dockerfile
├─ docker-compose.yml
└─ hello.py

index.html

<h1>Hello</h1>

hello.py

from flask import Flask, render_template ## render_templateを追加

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html') ## render_templateを使用

## 起動用
if __name__ == "__main__":
    app.run(debug=True)

以上!!!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?