我正在尝试在Docker容器中缺乏经验的小型Web应用程序在Docker容器中运行。该webapp使用来自表单的输入,呈现模板并将其作为文本文件返回给用户。
如果我在本地运行该应用程序,则一切正常。 如果我在Docker容器中运行应用程序,则可以访问每个静态站点。在表单中按“提交”时,出现“连接被拒绝”错误。 在Flask调试器中,我看不到任何事情发生,这使我相信,无论出于何种原因(缺少模块?),表单数据都永远不会将其发送到Flask或Flask。
根据研究,我认为不需要添加任何本地防火墙规则。我意识到Docker容器有点大,我可以使用alpine和gunicorn代替centos,但目前我并没有在追求效率。
Docker主机正在运行CentOS 7。
Requirements.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
在本地运行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安装程序的步骤还是我可以做其他调试工作?
答案 0 :(得分:0)
看来Docker配置有问题。我假设您正在使用Windows或其他版本的Docker,而不是Docker Toolbox。
运行docker ps
,然后查看“ PORTS”部分。 docker主机的localhost和docker容器的暴露端口之间应该存在绑定。该部分应显示为0.0.0.0:5000->5000/tcp
。
您不是在说容器的运行方式。要实现端口绑定,您应该添加-p
参数。例如docker run -p 5000:5000 image_name
我建议您阅读基本的Docker文档。它不太长,应该使您更清楚地了解其工作原理。
答案 1 :(得分:0)
Kendas的评论指出,很明显,表单操作指向无法正常运行的localhost。将表单操作更改为服务器的IP地址,现在一切正常。