在Docker容器中运行Flask应用程序 [英] Running Flask app in a Docker container
问题描述
我已经构建了一个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 /
但是我得到一个超时。有什么可能会出错?
从这里一切看起来不错。让我们来做一些调试。
让我们以名称开始我们的容器: c>标记并将日志直接显示在屏幕上。 首先检查Flask应用程序是否正在运行,或者可能已经崩溃。从一个简单的 如果在这里一切都很好,那么是时候进入容器。让我们尝试使用 I've built a Docker image containing a simple Flask test app: using the The Docker image was built using and I've run it using: I'm assuming that I can test the app by pointing the browser to Everything looks good from here. Let's do some debugging. Let's begin by starting our container with a name:
You can also avoid the First check if the Flask app is really running or maybe it has crashed. Start with a simple If everything looks good until here, then it's time to enter the container. Let's try with 这篇关于在Docker容器中运行Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
docker run -d -p 5000:5000 - 你也可以避免
-d $ c $ -b
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
,或者不管它叫什么名字)。from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')
Dockerfile
:FROM ubuntu:latest
RUN apt-get update -y
RUN 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 build -t flask-app .
and it has been successfully created:$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask-app latest fab0d79fd7ac 8 minutes ago 642.2 MB
ubuntu latest 104bec311bcd 5 weeks ago 129 MB
$ docker run -d -p 5000:5000 flask-app
e92b249dd02ca44489069b783fa9be713c7a14ea893061194e37c80f16d8c931
http://localhost:5000/
but I get a timeout. What could be going wrong?docker run -d -p 5000:5000 --name flask-app-test flask-app
-d
flag and get the logs right onto your screen.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?)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).