简体   繁体   English

如何“码头化” Flask应用程序?

[英]How to “dockerize” Flask application?

I have Flask application named as rest.py and I have dockerize but it is not running. 我有一个名为rest.py的Flask应用程序,并且我有dockerize,但它没有运行。

#!flask/bin/python
from flask import Flask, jsonify

app = Flask(__name__)
tasks = [
          {
            'id': 1,
            'title': u'Buy groceries',
            'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', 
            'done': False
          }
        ]

@app.route('/tasks', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

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

Dockerfile is as follows Dockerfile如下

FROM ubuntu 
RUN apt-get update -y
RUN apt-get install -y python-dev python-pip
COPY . /rest
WORKDIR /rest
RUN pip install -r Req.txt 
ENTRYPOINT ["python"]
CMD ["rest.py"]

I have build it using this command... 我已经使用此命令来构建它...

$ docker build -t flask-sample-one:latest

...and when I run container... ...当我运行容器时...

$ docker run -d -p 5000:5000 flask-sample-one 

returning the following output: 7d1ccd4a4471284127a5f4579427dd106df499e15b868f39fa0ebce84c494a42 What am I doing wrong? 返回以下输出:7d1ccd4a44712​​84127a5f4579427dd106df499e15b868f39fa0ebce8​​4c494a42我在做什么错?

I believe that you need to reinforce your concepts about Docker , in order to understand how it works, and then you will achieve your objectives regarding "dockerizing" whatever application. 我相信您需要加强有关Docker的概念,以了解其工作原理,然后才能实现有关“对任何应用程序进行“ Docker化”的目标。

Here is an article which can give your some first steps. 这是一篇可以给您一些初步步骤的文章

An official HOWTO will also help you. 官方的HOWTO也将为您提供帮助。

Some observations that might help you: 一些可以帮助您的观察:

  • check if your Req.txt contains flask for installation 检查您的Req.txt是否包含用于安装的flask
  • before dockerizing , check if your application is working 在进行dockerizing之前,请检查您的应用程序是否正常运行
  • check your running containers with docker ps and see if your container is running 使用docker ps检查您正在运行的容器,并查看您的容器是否正在运行
  • if it is running, test your application: curl http://127.0.0.1:5000/tasks * 如果正在运行,请测试您的应用程序: curl http://127.0.0.1:5000/tasks *

One more thing: 还有一件事:

  • your JSON has an OBJECT with an ARRAY with just one ELEMENT 您的JSON有一个数组对象只有一个元素

Is that what you want for your prototype? 这就是您想要的原型吗?

Take a look on this doc , about the JSON standard. 看一下有关JSON标准的文档

  1. The output you get is the container ID. 您得到的输出是容器ID。 Check with docker ps whether it keeps running. docker ps检查是否保持运行。

  2. Use docker logs [container-id] to figure out what's going on inside. 使用docker logs [container-id]找出内部发生了什么。

  3. Some problems I can find in your question: 我可以在您的问题中发现一些问题:

    1. Change the app.run line to app.run(host='0.0.0.0', debug=True) . app.run行更改为app.run(host='0.0.0.0', debug=True) From the point of view of the container, its services need to be externally available. 从容器的角度来看,其服务需要在外部可用。 So they need to run on the loopback interface, like you would run it if you'd set up a publicly available server on a host directly. 因此,它们需要在回送接口上运行,就像如果直接在主机上设置公共可用服务器那样运行它。

    2. Make sure that Flask gets installed. 确保已安装Flask。 Your docker image file requires all the commands to make it work from a blank Ubuntu installation. 您的docker映像文件需要所有命令才能从空白的Ubuntu安装中运行。

    3. Please do not forget to deactivate debug if you'd ever expose this service on your host. 如果您曾在主机上公开此服务, 请不要忘记停用调试。 Debug mode in Flask makes it possible for visitors to run arbitrary code if they can trigger an exception (it's a feature, not a bug). Flask中的调试模式使访问者可以触发任意代码(如果它们可以触发异常)(这是功能,而不是错误)。

After that (and building the container again [1]), try curl http://127.0.0.1:5000/tasks on the host. 之后(并再次构建容器[1]),尝试在主机上curl http://127.0.0.1:5000/tasks Let me know if it works, if not there are other problems in your setup. 让我知道它是否有效,如果没有,您的设置中还有其他问题。

[1] You can improve the prototyping workflow with Flask's built-in reloader (which is enabled by default) if you use a volume mount in your docker container for the directory that contains your python files - this would allow you to change your script on the host, reload in the browser and directly see the result. [1]如果您在包含您的python文件的目录的Docker容器中使用卷装载,则可以使用Flask的内置重新加载器(默认情况下启用)来改善原型工作流程。主机,在浏览器中重新加载并直接查看结果。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM