docker 容器上无法访问 flask-restplus 微服务

flask-restplus microservice not reachable on docker container

这是关于一个带有 echo 端点的简单 flask-restplus 微服务,可以在 here 中找到 默认情况下在端口 5000 上本地运行:

cd $src_folder
python app.py

但是当 运行 在带有简单 Dockerfile 的 docker 容器中时...

RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python", "./app.py" ]

...使用正确的端口 mapping/exposing,由于某种原因无法访问:

    docker run -d -h $HOST --name $CONTAINER \ 
--publish $SERVER_PORT:$CONTAINER_PORT $DOCKER_HUB_IMG:$IMAGE_VERSION

...即使容器日志显示它已正确启动:

(venv) .../flaskexample$ docker logs flaskexample 
2017-09-17 18:23:12,505 - werkzeug - INFO -  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
2017-09-17 18:23:12,506 - werkzeug - INFO -  * Restarting with stat
2017-09-17 18:23:13,016 - werkzeug - WARNING -  * Debugger is active!
2017-09-17 18:23:13,021 - werkzeug - INFO -  * Debugger PIN: 246-320-471
(venv) .../flaskexample$ 

...在 docker ps:

中看起来也不错
(venv) .../flaskexample$ docker ps
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                                                                    NAMES
9d90da7ae192        kakicode/flaskexample:latest   "python ./app.py"        4 minutes ago       Up 4 minutes        0.0.0.0:5000->5000/tcp                                                   flaskexample

..但出于某种原因我不断收到:

...我 运行 没有想法...不得不说我没有在 flask-restplus 配置中提供 SERVER_NAME,因为它没有任何区别,仍然遇到同样的问题,而且所有其他需要端口 mapping/exposing 的容器在我的 docker 守护程序中工作正常。 我在 运行 这些实验中 Ubuntu 14.04.5.

...有没有人遇到过与 flask-restplus 类似的事情?

提前谢谢大家

您的问题是您会在 app.py

中使用如下内容
app.run(debug=True, port=5000)

app.run()

它的作用是默认监听 127.0.0.1。但是要使端口映射起作用,它必须监听容器内的所有接口。所以你应该使用

app.run(debug=True, port=5000, host="0.0.0.0")

如需进一步参考,请参阅:

http://flask.pocoo.org/docs/0.12/api/?highlight=run#flask.Flask.run