[英]How can I make a dockerized Flask app write output to file?
I'm trying to make the server which hosts a (dockerized) Flask app "log" requests that are sent to it, simply by writing them to a text file. 我试图通过简单地将它们写入文本文件的方式,使托管了(码头化的)Flask应用程序的服务器“日志”请求发送到该服务器。
My docker-compose.yml
looks like 我
docker-compose.yml
看起来像
version: '2'
services:
writer:
build: writer/
ports:
- 5000:5000
container_name: writer
and inside the writer
directory I have the writer.py
file as follows, which is supposed to get the request and write it to a local file feedback.txt
: 在
writer
目录中,我有如下writer.py
文件,该文件应用于获取请求并将其写入本地文件feedback.txt
:
from flask import Flask, request, jsonify
app = Flask(__name__, static_url_path='')
@app.route('/feedback', methods=['POST'])
def log_feedback():
with open("feedback.txt","a") as fo:
fo.write(request.data.decode("utf-8"))
print(request.data.decode("utf-8"))
fo.write('\n')
return 'Got it!'
if __name__ == '__main__':
app.run(host='0.0.0.0', debug = False)
For reference, my Dockerfile
in the same writer
dir is: 作为参考,我在同一
writer
目录中的Dockerfile
是:
FROM python:3.6-slim
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
ENTRYPOINT [ "python", "writer.py" ]
requirements.txt
is simply requirements.txt
只是
flask
And a sample request is 样品要求是
curl -H "Content-Type:application" -X POST -d '{"Content":"Hello world"}' http://0.0.0.0:5000/feedback
Unfortunately, no feedback.txt
gets written locally (eg outside the container). 不幸的是,没有
feedback.txt
被本地写入(例如,在容器外部)。 Can you hint to me the modifications I should perform eg in docker-compose.yml
(eg with volumes?) in order to get the feedback.txt
written and accessible on the server? 您能否向我暗示我应该在
docker-compose.yml
执行的修改(例如,带有卷?),以便在服务器上编写并访问feedback.txt
?
You have to add a volume in the docker-compose.yml. 您必须在docker-compose.yml中添加一个卷。 The path in the container should contain the folder that contains the written file.
容器中的路径应包含包含写入文件的文件夹。 The path to local folder should contain a folder on your system where you want to see the written file.
本地文件夹的路径应包含您系统上要查看写入文件的文件夹。
version: '2'
services:
writer:
build: writer/
ports:
- 5000:5000
container_name: writer
volumes:
- /PATH/TO/YOUR/LOCAL_FOLDER:/PATH/TO/FOLDER/IN/CONTAINER
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.