首页 > 解决方案 > 在 Azure 上工作但不在本地工作的 Docker 中的烧瓶

问题描述

这是我的代码(app.py):

from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello():
    return "Hello World composcan"


if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0', port=8002)

当我使用这个 dockerfile 时:

FROM ubuntu:18.04
MAINTAINER bussiere "bussiere@composcan.fr"
RUN echo "nameserver 8.8.8.8" >> /etc/resolv.conf
RUN echo "nameserver 80.67.169.12" >> /etc/resolv.conf
RUN echo "nameserver 208.67.222.222" >> /etc/resolv.conf
#RUN echo "dns-nameservers 8.8.8.8 8.8.4.4 80.67.169.12 208.67.222.222" >>  /etc/network/interfaces
ENV LANG C.UTF-8
RUN apt-get update -y
RUN apt-get install -y --no-install-recommends apt-utils
RUN apt-get install -y python3 python3-pip python3-dev build-essential
RUN python3 -m pip install pip --upgrade
RUN python3 -m pip install pipenv
RUN export LC_ALL=C.UTF-8
RUN export LANG=C.UTF-8
COPY app /app
WORKDIR /app
RUN pipenv --python 3.6
RUN pipenv install -r requirements.txt
ENTRYPOINT ["pipenv"]
CMD ["run","python","app.py"]

它完美地适用于天蓝色:

http://koalabourre.azurewebsites.net/

但是当我尝试从 ubuntu 上的 docker 本地运行它时:

docker run --rm -it -p 8002:8002 flask-quickstart

我有 :

* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:8002/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 101-413-323

我无法使用 localhost:8002 在浏览器中打开它

这是项目的组织:

在此处输入图像描述

码头工人在这里 https://hub.docker.com/r/dockercompo/koalabourre/

并且在本地容器外运行 app.py 可以完美运行...

标签: azuredockerflask

解决方案


您必须监听“外部”(docker 网络)地址才能转发端口。在我看来,您的代码在这方面与程序的输出不太匹配。

你的代码说

app.run(debug=True,host='0.0.0.0', port=8002)

但是你的输出说

 * Running on http://127.0.0.1:8002/ (Press CTRL+C to quit)

推荐阅读