3
8

More than 3 years have passed since last update.

Dockerfile + docker-compose.ymlでFlask環境構築

Posted at

手軽にflask環境を構築したいときの備忘録として。

実行環境

  • Mac OSX 10.15.4
  • Docker version 19.03.8

ディレクトリ構成

test/
   |- Dockerfile
   |- docker-compose.yml
   |- app/
      |- app.py

各ファイルの記述

Dockerfile

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

以上で準備完了。

ビルド→コンテナ起動→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
3
8
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
8