在Docker容器中运行Flask应用程序

在Docker容器中运行Flask应用程序

本文介绍了在Docker容器中运行Flask应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经构建了一个Docker镜像,其中包含一个简单的Flask测试应用程序: from flask import Flask app = Flask(__ name__) @ app.route(/) def hello_world():返回Hello World! if __name__ ==__main__: app.run(debug = True,host ='0.0.0.0') Dockerfile : $ b $ FROM ubuntu:latest 运行apt-get update -y 运行apt-get install -y python -pip python-dev build-essential COPY。 / app WORKDIR / app RUN pip install -r /app/requirements.txt ENTRYPOINT [python] CMD [app.py] Docker镜像是使用 docker build -t flask-app。 $ docker images 存储库标记图像ID创建的大小 flask-app最新fab0d79fd7ac 8分钟前642.2 MB ubuntu latest 104bec311bcd 5周前129 MB 我已经运行它: $ docker run -d -p 5000:5000 flask-app e92b249dd02ca44489069b783fa9be713c7a14ea893061194e37c80f16d8c931 我假设我可以通过将浏览器指向 http:// localhost:5000 / 但是我得到一个超时。有什么可能会出错?解决方案从这里一切看起来不错。让我们来做一些调试。 让我们以名称开始我们的容器: docker run -d -p 5000:5000 - 你也可以避免 -d c>标记并将日志直接显示在屏幕上。 首先检查Flask应用程序是否正在运行,或者可能已经崩溃。从一个简单的 docker ps 开始(看看你是否注意到有什么奇怪的地方),然后是 docker logs flask-app-test (这些将是Flask应用程序的日志,它崩溃了吗?有什么奇怪的吗?) 如果在这里一切都很好,那么是时候进入容器。让我们尝试使用 docker exec -ti flask-app-test bash 。进入后,通过 apt-get install wget 安装 wget ,然后测试web服务器是否从内部工作 wget http:// localhost:5000 和 cat - 希望下载的文件( ls 后跟 cat file.html ,或者不管它叫什么名字)。 I've built a Docker image containing a simple Flask test app:from flask import Flaskapp = Flask(__name__)@app.route("/")def hello_world(): return "Hello World!"if __name__ == "__main__": app.run(debug=True,host='0.0.0.0')using the Dockerfile:FROM ubuntu:latestRUN apt-get update -yRUN apt-get install -y python-pip python-dev build-essentialCOPY . /appWORKDIR /appRUN pip install -r /app/requirements.txtENTRYPOINT ["python"]CMD ["app.py"]The Docker image was built using docker build -t flask-app . and it has been successfully created:$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEflask-app latest fab0d79fd7ac 8 minutes ago 642.2 MBubuntu latest 104bec311bcd 5 weeks ago 129 MBand I've run it using:$ docker run -d -p 5000:5000 flask-appe92b249dd02ca44489069b783fa9be713c7a14ea893061194e37c80f16d8c931I'm assuming that I can test the app by pointing the browser to http://localhost:5000/ but I get a timeout. What could be going wrong? 解决方案 Everything looks good from here. Let's do some debugging.Let's begin by starting our container with a name:docker run -d -p 5000:5000 --name flask-app-test flask-appYou can also avoid the -d flag and get the logs right onto your screen.First check if the Flask app is really running or maybe it has crashed. Start with a simple docker ps (see if you notice anything strange there) followed by docker logs flask-app-test (these will be the logs of the Flask app, has it crashed? is there aything strange?)If everything looks good until here, then it's time to enter the container. Let's try with docker exec -ti flask-app-test bash. Once you are in, install wget by apt-get install wget and then test if the webserver works from the inside by doing wget http://localhost:5000 and by cat-ting the hopefully downloaded file (ls followed by cat file.html, or whatever it's called). 这篇关于在Docker容器中运行Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-07 08:30