从主机无法访问在Docker容器中运行的flask应用程序

时间:2020-02-02 12:07:26

标签: docker flask

这是我的Dockerfile:

FROM ubuntu:latest

ENV LANG C.UTF-8 
ENV LC_ALL C.UTF-8
ENV VIRTUAL_ENV=/opt/flask_venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV FLASK_ENV development

RUN apt update -y && apt upgrade -y && apt install -y python3 python3-venv net-tools
RUN python3 -m venv $VIRTUAL_ENV

COPY . /opt/my_app
WORKDIR /opt/my_app
RUN python3 -m pip install wheel
RUN python3 -m pip install -r requirements.txt

EXPOSE 5656
ENTRYPOINT [ "python3" ]
CMD ["app.py"]

这是运行容器的方式:

docker run --name flask_container -d -p 5001:5656 flask_app:0.0.1

这是我的app.py

if __name__ == "__main__":
    app.run(ssl_context="adhoc", host="0.0.0.0", port=5656)

当我在浏览器中转到https://localhost:5001时,无法与该应用建立连接(Chrome:无法访问此网站)。

我要去哪里错了?

编辑

完成app.py

#!/usr/bin/env python3
import os

from flask_migrate import Migrate

from app_directory import app_factory, db
from cli_commands import register

app = app_factory(os.getenv("ENV_VAR") or "dev")
migrate = Migrate(app, db)

register(app, db)

if __name__ == "__main__":
    app.run(ssl_context="adhoc", host="0.0.0.0", port=5656)

2 个答案:

答案 0 :(得分:0)

如果要调试docker容器,建议不要使用-d标志,除非您完全确定一切正常。

对于您而言,如果删除了-d标志,则会看到您的python脚本不完整:

docker run --rm --name flask_container -p 5001:5656 so
Traceback (most recent call last):
  File "app.py", line 5, in <module>
    app.run(ssl_context="adhoc", host="0.0.0.0", port=5656)
NameError: name 'app' is not defined

一种快速解决方案是拥有以下app.py

from flask import Flask
app = Flask(__name__)

if __name__ == "__main__":
    app.run(ssl_context="adhoc", host="0.0.0.0", port=5656)

重建图像后,现在导致:

docker run --rm --name flask_container -p 5001:5656 so
 * Serving Flask app "app" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on https://0.0.0.0:5656/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 423-289-631

现在您可以使用-d重新启动容器:)

docker run --rm --name flask_container -d -p 5001:5656 so
2c777b21c96f080c05df714c339c80ca88c9943e64cc0de12d05544c3d81faba
apoehlmann:~/workspace/teststuff/stackoverflow$ docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                               NAMES
2c777b21c96f        so                   "python3 app.py"         3 seconds ago       Up 1 second         0.0.0.0:5001->5656/tcp              flask_container

答案 1 :(得分:0)

我可以使用此dockerfile:

# docker build . -t flask_app
# docker run --name flask_container -p 4567:5000 flask_app
FROM ubuntu:latest

ENV LANG C.UTF-8 
ENV LC_ALL C.UTF-8
ENV VIRTUAL_ENV=/opt/flask_app/flask_venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV FLASK_ENV development

RUN apt update -y && apt upgrade -y && apt install -y python3 python3-venv

RUN python3 -m venv $VIRTUAL_ENV

RUN groupadd -r flask_group && useradd -r -m -g flask_group flask_user

COPY . /opt/flask_app/my_app
WORKDIR /opt/flask_app/my_app
RUN python3 -m pip install wheel
RUN python3 -m pip install -r requirements.txt

RUN chown -R flask_user:flask_group /opt/flask_app
USER flask_user

ENTRYPOINT [ "python3" ]
CMD ["app.py"]

我认为暴露港口是有问题的,但我仍然不是问题的100%。