2
4

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.

python + Flask + docker でwebアプリケーションを動かす

Last updated at Posted at 2023-07-27

概要

EC2インスタンス上でdockerとdocker-composeをインストールして動かす方法メモ
OS: amazonLinux2

ドキュメント

https://flask.palletsprojects.com/en/2.0.x/tutorial/
https://flask.palletsprojects.com/en/2.0.x/installation/
https://flask.palletsprojects.com/en/2.0.x/quickstart/

dockerインストール

インストール -> 起動 -> ec2-userでも動かせるように権限調整

$ sudo su -
# yum update -y
# amazon-linux-extras install -y docker
# systemctl enable docker
# systemctl start docker
# usermod -aG docker ec2-user

docker-composeインストール

docker-composeのバージョンは適時調整する
バージョン情報まで表示できたらOK!

# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose
# chmod 755 /usr/local/bin/docker-compose
# docker-compose -v
docker-compose version 1.24.1, build 4667896b

pythonコンテナを作ってアクセス確認

以下はec2-userで操作

$ mkdir project
$ cd project/
$ mkdir -p docker/python

$ vi docker/python/Dockerfile
$ vi docker-compose.yml
$ vi docker/python/app.py 
$ vi docker/python/templates/index.html

$ docker-compose up -d
docker/python/app.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=5000)
docker-compose.yml
version: "3"

services:
  python:
    build: ./docker/python
    ports:
      - 80:5000
docker/python/Dockerfile
FROM python:3.9
WORKDIR /app
ADD . /app
RUN pip install Flask
EXPOSE 5000
CMD ["python", "app.py"]
docker/python/templates/index.html
<html>
    <title>hogehoge</title>
    <head>
    </head>

    <body>
        hogehoge
    </body>
</html>

ゴール

パブリックIPに80ポートでアクセスして、「hogehoge」が表示されるページが出ればok

追加

この記事で触れた内容を自前の環境で動かしてみる

docker/python/app.py
from flask import Flask, render_template
import numpy as np
from matplotlib.figure import Figure
import base64
from io import BytesIO

app = Flask(__name__)

@app.route('/')
def index():
    np.random.seed(1)
    x = np.arange(10)
    y = np.random.randint(1, 1000, 10)

    fig = Figure()
    ax = fig.subplots()
    ax.plot(x, y)

    buf = BytesIO()
    fig.savefig(buf, format="png")

    data = base64.b64encode(buf.getbuffer()).decode("ascii")
    return render_template('index.html', data=data)

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=5000)
docker/python/Dockerfile
FROM python:3.9
WORKDIR /app
ADD . /app
RUN pip install Flask\
    & pip install numpy\
    & pip install matplotlib
EXPOSE 5000
CMD ["python", "app.py"]
docker/python/templates/index.html
<html>
  <title>hogehoge</title>
  <head>
  </head>

  <body>
    <img src="data:image/png;base64,{{data}}"/>
  </body>
</html>
2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?