简体   繁体   English

在 docker 容器上运行时,Flask 应用程序无法正常工作

[英]Flask App not working when running on docker container

Im new to docker and Flask and I'm getting an issue when I try to run the app.我是 docker 和 Flask 的新手,当我尝试运行该应用程序时遇到了问题。 The browser says the site (172.17.0.2:5000) can't be reached.浏览器说无法访问该站点 (172.17.0.2:5000)。 For anyone wondering, the Dockerfiles:对于任何想知道的人,Dockerfiles:

FROM ubuntu

RUN apt-get update && apt-get install -y python3 python3-pip

RUN pip3 install flask

RUN mkdir -p /opt/MyApp-test

COPY . /opt/MyApp-test

WORKDIR /opt/MyApp-test

EXPOSE 5000

ENTRYPOINT python3 main.py
CMD flask run --host 0.0.0.0

The main.py:主要.py:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return 'IT WORKED! I AM RUNNING FROM A DOCKER CONTAINER!!!'


if __name__ == '__main__':
    app.run()

And when I run the container, I get:当我运行容器时,我得到:

(base) daniellombardi@Daniels-MacBook-Pro MyApp-test % docker run 2625
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

I try to go to http://127.0.0.1:5000/ , unable to connect, then I inspect the container to get its IP address, and it says 172.0.1 instead of 127.0.0.1:5000, but also unable to connect.我尝试访问http://127.0.0.1:5000/ ,无法连接,然后我检查容器以获取其 IP 地址,它显示 172.0.1 而不是 127.0.0.1:5000,但也无法连接. And the app works when I run it on my computer.当我在我的计算机上运行该应用程序时,它就可以工作。

在调用 docker run 时,您应该将容器内暴露的端口绑定到 docker 主机中的端口,因此 docker run 应该这样调用:

docker run 2625 -p 5000:5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Docker容器中运行Flask应用程序 - Running Flask app in a Docker container 在docker容器内的virtualenv上运行flask应用 - Running flask app on virtualenv inside docker container Docker容器在运行flask app后立即退出 - Docker container exits immediately after running flask app 如何将 CSV 文件传递​​给在 Docker 容器内运行的 Flask 应用程序? - How to pass a CSV file to a Flask app running inside a Docker container? Flask + Docker应用-当我运行docker run时容器死亡 - Flask + Docker app - The container dies when i run docker run 在 docker 容器内运行 flask 应用程序时无法访问端点 - Not able to access endpoints when running flask application inside docker container 如何将Flask应用程序docker容器连接到本地mongoDB(不是docker容器) - How to connect Flask app docker container to local mongoDB (not docker container) 在 Docker 容器中运行 Flask 应用程序会导致“./boot.sh: 2: exec: gunicorn: not found” - Running a Flask app within a Docker container results in “./boot.sh: 2: exec: gunicorn: not found” 无法返回静态文件 [404] 在 Docker (Ubuntu) 容器中运行的 Flask 应用程序 - Unable to Return Static files [404] Flask App Running in Docker (Ubuntu) Container 在Docker容器中使用uwsgi和nginx设置Flask应用 - Setting flask app with uwsgi and nginx in docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM