[英]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.