https://pythonise.com/series/learning-flask/building-a-flask-app-with-docker-compose

以下配置摘自flask-web编程一书

dockerfile:

FROM python:3.6-alpine

ENV FLASK APP flasky.py
ENV FLASK CONFIG docker

RUN adduser -D flasky
USER flasky

WORKDIR /home/flasky

COPY requirements requirements
RUN python -m venv venv
RUN venv/bin/pip install -r requirements/docker.txt
COPY app app

COPY migrations migrations
COPY flasky.py config.py boot.sh ./

运行时配置
EXPOSE 5000
ENTRYPOINT [“./boot.sh”]

config.py:
这个新配置类继承自ProductionConfig,只不过把日志重定向到stderr。Docker将自动从中捕获日志,通过docker logs命令对外输出。

class DockerConfig(ProductionConfig):
@classmethod
def init app(cls, app):
ProductionConfig.init app(app)


# 把日志输出到stderr
import logging
from logging import StreamHandler
file handler = StreamHandler()
file handler.setLevel(logging.INFO)
app.logger.addHandler(file handler)


config = {
# …
‘docker’: DockerConfig,
# …
}