With the popularity of containerization technology, Docker has become the preferred platform for the development and deployment of many cloud-native applications. One of the common application scenarios is the interaction between the container and external resources, such as uploading and downloading files, etc. But sometimes when using a Docker container to upload files, you may find that the file stream cannot be read. This article will explore why this happens and how to fix it.
1. Problem description
When I used the Docker container to upload files, I found that the upload was successful, but there was a problem when reading the file stream, and the file stream was empty. I have tried a variety of methods, including using local file reading and remote file reading, but can't solve it. The following is my code example:
import io from datetime import datetime from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload_file(): f = request.files['file'] stream = io.StringIO() stream.write(f.stream.read()) stream.seek(0) print(stream.read()) return jsonify({'success': True}) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)
When using the curl
command to upload a file, you can see that the upload is successful, but when reading the file stream it is empty:
$ curl -X POST -F "file=@test.txt" http://localhost:5000/upload {"success": true} $ python app.py 2019-12-26 16:10:10,990 - INFO - * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 2019-12-26 16:10:16,930 - INFO -
2. Problem Analysis
Inside the container, you can use the docker cp
command to copy the file to the inside of the container for testing. It can be found that there is no problem with file stream reading. So the problem is obviously not with the code that reads the file stream.
I checked some information and found that Docker will use TempFS as the temporary storage directory for uploaded files when processing uploaded files. The possible reason is that the file system isolation inside the container prevents the uploaded file from being read.
3. Solution
To solve this problem, you need to use any of the three methods:
--privileged
to run Container enables privileged mode, that is, uses --privileged
to run the container. This option will give the container root access to the host.
$ docker run --privileged -d -p 5000:5000 my-image
--tmpfs /path/to/tmpfs:rw
option Use --tmpfs
mount The temporary file system ensures that uploaded files can be read correctly.
$ docker run -d -p 5000:5000 --tmpfs /tmp:rw my-image
Directly mount the directory in the host file system to a certain path of the container, which can make it possible both inside and outside the container Uploaded files are read correctly.
$ docker run -d -p 5000:5000 -v /path/to/host/drive:/path/to/container/drive my-image
Use any of the above methods and upload the test again. You can see that both uploading and reading the file stream are successful:
$ curl -X POST -F "file=@test.txt" http://localhost:5000/upload {"success": true} $ python app.py 2019-12-26 16:55:01,697 - INFO - * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 2019-12-26 16:55:03,428 - INFO - This is a test file.
In short, Docker’s file system isolation mechanism sometimes causes files to be uploaded. It cannot be read correctly, but there are three solutions above. I hope this article can help you with the problems you encounter when using Docker applications.
The above is the detailed content of How to solve the problem that the docker upload file stream cannot be read. For more information, please follow other related articles on the PHP Chinese website!