Skip to content

Instantly share code, notes, and snippets.

@ftnext
Last active July 29, 2018 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftnext/0d578da76d3d3d0adb60e18f5c02ddb9 to your computer and use it in GitHub Desktop.
Save ftnext/0d578da76d3d3d0adb60e18f5c02ddb9 to your computer and use it in GitHub Desktop.
Flask HelloWorld Dockerイメージ
from flask import Flask
app = Flask(__name__)
from app import routes
#!/bin/sh
source venv/bin/activate
# Chapter17:
# -b リクエストを待ち受けるポート設定
# [the module that contains the application]:[the name of this application]
# アクセスログとエラーログはDockerのログとして標準出力に送られるように設定
exec gunicorn -b :5000 --access-logfile - --error-logfile - microblog:app
# Mega Tutorial Chapter19
# 参考: https://qiita.com/RyosukeKamei/items/5e834fef3b5a187e50c4
# もととなる公式イメージ
FROM python:3.6-alpine
# アプリケーションを実行するためのユーザを作成(rootで実行するのはよくないとのこと)
# -Dオプションはデフォルトの設定
RUN adduser -D microblog
# アプリケーションがインストールされるデフォルトディレクトリ(microblogユーザのホームディレクトリを使っている)
WORKDIR /home/microblog
# Mega Tutorial作成者の好み(#7のコメント。python3とpip3というコマンドを使わなくてよくなる)
COPY requirements.txt requirements.txt
RUN python -m venv venv
# pip install --upgrade pipを実行するとよさそう
RUN venv/bin/pip install -r requirements.txt
RUN venv/bin/pip install gunicorn
COPY app app
COPY microblog.py boot.sh ./
RUN chmod +x boot.sh
ENV FLASK_APP microblog.py
RUN chown -R microblog:microblog ./
USER microblog
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]
from app import app
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
@ftnext
Copy link
Author

ftnext commented Jun 24, 2018

  • ├ app/
    • __init__.py
    • └ routes.py
  • ├ boot.sh
  • ├ Dockerfile
  • ├ microblog.py
  • └ requirements.txt

@ftnext
Copy link
Author

ftnext commented Jul 29, 2018

上記のイメージはローカル(Docker for Mac)では動くが、AzureのWebAppsでは動かない。
動かなかった原因はベースイメージらしい。alpineをやめてpython:3.6に変えたらAzureでも動いた。
http://nikkie-ftnext.hatenablog.com/entry/2018/07/02/000426

原因調査の過程で次のイメージを試した。
https://github.com/danriti/gunicorn-flask
そのままならAzureでも動いたが、microblogの設定に変更するとAzureでは動かなくなる(Python2系、3系混在による事象と見ている)

試行錯誤したDockerfile(結局動かない)

# gunicorn-flask

FROM ubuntu:16.04
#MAINTAINER Daniel Riti <dmriti@gmail.com>

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
#RUN apt-get install -y python python-pip python-virtualenv gunicorn
RUN apt-get install -y python3 python3-pip gunicorn

# Setup flask application
RUN mkdir -p /deploy/app
COPY gunicorn_config.py /deploy/gunicorn_config.py
# add for debug
COPY boot.sh /deploy/boot.sh
COPY app /deploy/app
RUN pip3 install -r /deploy/app/requirements.txt
WORKDIR /deploy/app

EXPOSE 5000

# Start gunicorn
CMD ["/usr/bin/gunicorn", "--config", "/deploy/gunicorn_config.py", "hello:app"]
# ENTRYPOINT ["./boot.sh"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment