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.
My docker-compose.yml
looks like
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
:
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:
FROM python:3.6-slim
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
ENTRYPOINT [ "python", "writer.py" ]
requirements.txt
is simply
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). 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?
You have to add a volume in the 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.