Python + Flask + Docker 開発環境構築

python初心者。
pythonだと venvpipenv があるようなのですが、
次の現場がdockerを使っているようなので勉強がてら設定。

PHPではdockerでやっていたので、統一するっということでは良いのかも。

dockerはインストールされている前提。

スポンサードサーチ

imageのインストール

後述のDockerfileに記載するので、ここは飛ばしてもよいが一応記載。

dockerhubより
https://hub.docker.com/_/python

% docker pull python
Using default tag: latest
latest: Pulling from library/python
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
0a5e725150a2: Pull complete
397dba5694db: Pull complete
b1d09d0eabcb: Pull complete
475299e7c7f3: Pull complete
f7f508209c90: Pull complete
Digest: sha256:c2ddd6d8118c92ccee2b181f7a6f64ea50f003b697500971303ab7fea4b385a8
Status: Downloaded newer image for python:latest
docker.io/library/python:latest
% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest 18f3593da4a5 34 hours ago 886MB

dockerでpython環境の用意

FROM python:latest

WORKDIR /var/www/app
version: "3"

services:
  app:
    build:
      context: .
      dockerfile: ./docker/python/Dockerfile
    volumes:
      - ./:/var/www/app:delegated
    tty: true

コンテナの起動

% docker-compose build
% docker-compose up -d

pythonが入っているか?

% docker-compose exec app python3 --version
Python 3.9.0

pipが入っているか?

% docker-compose exec app pip --version
pip 20.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Flaskをインストール

Flask公式より、
https://flask.palletsprojects.com/en/1.1.x/installation/#install-flask

Dockerfileに pip install Flask を追加

FROM python:latest

WORKDIR /var/www/app

RUN pip install Flask

docker再起動後

% docker-compose exec app flask --version
Python 3.9.0
Flask 1.1.2
Werkzeug 1.0.1

Hello World の準備

Flask公式より、hello.py を作成
https://flask.palletsprojects.com/en/1.1.x/quickstart/#a-minimal-application

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

Hello World 実行 その1

コンテナに潜り実行

% docker-compose exec app bash
% export FLASK_APP=hello.py
% flask run
 * Serving Flask app "hello.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

http://127.0.0.1:5000/ にアクセス

アクセスできなかった \(^o^)/

Hello World 実行 その2

port: 5000が指定されるけれど、port開けてないよー。

% docker-compose ps
     Name     Command   State   Ports
-----------------------------------------
     app_1    python3   Up

docker-compose.ymlports: 追記

version: "3"

services:
  app:
    build:
      context: .
      dockerfile: ./docker/python/Dockerfile
    ports:
      - 5000:5000
    volumes:
      - ./:/var/www/app:delegated
    tty: true

dockerを再起動し、portを確認

% docker-compose ps
       Name           Command   State            Ports
--------------------------------------------------------------
      app_1           python3     Up      0.0.0.0:5000->5000/tcp

IPが127.0.0.1ではないので実行時に指定する。

% export FLASK_APP=hello.py
% flask run --host=0.0.0.0
 * Serving Flask app "hello.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

http://0.0.0.0:5000/ にアクセス

やった! \(^o^)/

また、flask run でport番号も指定したい場合は、 --port オプションをつければよさそう

% flask run --help
Usage: flask run [OPTIONS]

  Run a local development server.

  This server is for development purposes only. It does not provide the
  stability, security, or performance of production WSGI servers.

  The reloader and debugger are enabled by default if FLASK_ENV=development
  or FLASK_DEBUG=1.

Options:
  -h, --host TEXT                 The interface to bind to.
  -p, --port INTEGER              The port to bind to.
  --cert PATH                     Specify a certificate file to use HTTPS.
  --key FILE                      The key file to use when specifying a
                                  certificate.

  --reload / --no-reload          Enable or disable the reloader. By default
                                  the reloader is active if debug is enabled.

  --debugger / --no-debugger      Enable or disable the debugger. By default
                                  the debugger is active if debug is enabled.

  --eager-loading / --lazy-loader
                                  Enable or disable eager loading. By default
                                  eager loading is enabled if the reloader is
                                  disabled.

  --with-threads / --without-threads
                                  Enable or disable multithreading.
  --extra-files PATH              Extra files that trigger a reload on change.
                                  Multiple paths are separated by ':'.

  --help                          Show this message and exit.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です