繁体   English   中英

Flask 处理 Docker 容器中的表单 POST 请求

[英]Flask handling of form POST request in Docker Container

我正在尝试让一个小型 webapp 在 Docker 容器中运行,而在 Docker 方面的经验很少。 webapp 使用来自表单的输入,呈现模板并将其作为文本文件返回给用户。

如果我在本地运行应用程序,一切都会按预期工作。 如果我在 Docker 容器中运行应用程序,我可以访问每个 static 站点。 在表单中按“提交”时,我收到“连接被拒绝”错误。 In the Flask debugger I don't see anything happening which leads me to believe that either the form data never makes it to Flask or Flask for whatever reason (missing module?) fails to send me the file in Docker.

通过研究,我认为我不需要添加任何本地防火墙规则。 我意识到 Docker 容器有点大,我可以使用 alpine 和 gunicorn 代替 centos 但此时我不是在寻找效率。

Docker 主机正在运行 CentOS 7。

要求.txt:

Flask==1.1.2

Docker 文件:

FROM centos:centos7

COPY ./requirements.txt /app/requirements.txt
RUN yum install -y python3 python3-pip python3-devel

WORKDIR /app
RUN pip3 install --no-cache-dir -r requirements.txt
COPY . /app

ENTRYPOINT ["python3"]
CMD ["flaskapp.py" ]

Docker 启动指令

docker run -d -p 5000:5000 flaskapp

Output 在本地运行 python3 flaskapp.py 并提交表单时

PS c:\user> python3.exe .\flaskapp.py
 * Serving Flask app "flaskapp" (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: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 205-510-917
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [17/Jun/2020 15:31:28] "[37mGET /generator HTTP/1.1[0m" 200 -
127.0.0.1 - - [17/Jun/2020 15:32:00] "[37mPOST /result HTTP/1.1[0m" 200 -

Docker 容器中的相同操作

]# docker run -p 5000:5000 flaskapp
 * Serving Flask app "flaskapp" (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: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 161-592-324
10.10.10.10 - - [17/Jun/2020 13:30:10] "GET /generator HTTP/1.1" 200 -

html中的表单动作

<form action = "http://localhost:5000/result" method = "POST">
    <p><input type = "submit" value = "submit" /></p>

Flaskapp.py 缩短为仅包含我的 send_file 用法

from flask import Flask, render_template, request, send_file
import ipaddress
import zipfile
from pathlib import Path

app = Flask(__name__)

[...]

# generate configuration when data is received from generator form
@app.route('/result', methods=['POST', 'GET'])
def result():
    [...] some code, input checks, text manipulation

        # check folder content for .cfg files and add them to a zip archive
        with zipfile.ZipFile(temp_zipfile, mode='w') as z:
            for file in base_path.iterdir():
                if file.suffix == ".cfg":
                    try:
                        z.write(file, file, zipfile.ZIP_DEFLATED)
                    except:
                        continue
        # send the zip file to the user
        return send_file(temp_zipfile, as_attachment=True, mimetype='application/zip',attachment_filename="configurations.zip")
    # if HA is not used, send a cfg file directly
    elif result["cluster"] == "no":
        configfile = config_filepath + result["HOSTNAME"] + ".cfg"
        with open(configfile, "w+") as cfg:
            cfg.write(content)
        return send_file(configfile, as_attachment=True,attachment_filename='configurations.cfg')

我是否错过了 Docker 设置中的一个步骤,或者我可以进行额外的调试吗?

看起来 Docker 配置有问题。 我假设您将 Docker 用于 Windows 或替代方案,而不是 Docker 工具箱。

运行docker ps并查看端口部分。 docker 主机的 localhost 和 docker 容器的暴露端口之间应该有绑定。 该部分应为0.0.0.0:5000->5000/tcp

你并不是说你是如何运行你的容器的。 要实现端口绑定,您应该添加-p参数。 一个例子是docker run -p 5000:5000 image_name

我建议阅读基本的 Docker 文档。 它不会太长,应该让您更清楚地了解它是如何工作的。

Kendas 的评论指出了明显的问题,表单操作指向 localhost 无法正常工作。 将表单操作更改为服务器的 IP 地址,现在一切正常。

暂无
暂无

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

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