Can';t使用Flask和Docker卷写入文件
我正试图用Flask和docker写入一个文件。 如果在运行之前文件中有任何内容,则该文件将被清空并用空白页覆盖。但我的烧瓶应用程序实际上无法写入它 我认为我的卷设置不正确。以下是我所拥有的: 在我的道路上,我有: 矩阵/sampleMatrix.txt 静止的/ 模板/ app.py Dockerfile docker-compose.ymlCan';t使用Flask和Docker卷写入文件,docker,flask,Docker,Flask,我正试图用Flask和docker写入一个文件。 如果在运行之前文件中有任何内容,则该文件将被清空并用空白页覆盖。但我的烧瓶应用程序实际上无法写入它 我认为我的卷设置不正确。以下是我所拥有的: 在我的道路上,我有: 矩阵/sampleMatrix.txt 静止的/ 模板/ app.py Dockerfile docker-compose.yml version: '2' services: <container name>: build: . ports:
version: '2'
services:
<container name>:
build: .
ports:
- 82:80
container_name: <container name>
volumes:
#- ./matrices:/var/lib/docker/matrices
- ./matrices:/matrices
Dockerfile
FROM python:3.6.1-alpine
ADD . .
RUN pip install -r requirements.txt
RUN pip install requests
#VOLUME [ "./matrices" ] <- Do I need this? Doesn't seem to do anything more than the compose file
WORKDIR /
CMD ["python","app.py"]
来自python:3.6.1-1的
添加
运行pip安装-r requirements.txt
运行pip安装请求
#卷[“/Matrix”]要读取文件,请使用r
打开它
使用w+
打开文件将删除其内容
您可以阅读有关打开文件模式的更多信息非常感谢。工作完美。甚至没有想到这可能是问题所在
# This first block gets files in the matrices folder and confirms that my path matches a file which is there
for item in glob.glob("/matrices/*"):
print(item, file=sys.stderr)
fileName= "/matrices/sampleMatrix.txt"
print(item==fileName, file=sys.stderr)
fileName= "/matrices/sampleMatrix.txt"
print("Final path chosen: ",fileName, file=sys.stderr)
try:
#READ <- reads nothing even when I've written something in the file ahead of time
matrixFile= open(fileName, "w+")
print("About to read: ",fileName, file=sys.stderr)
print(matrixFile.read(), file=sys.stderr)
print("Read from: ",fileName, file=sys.stderr)
matrixFile.close()
#WRITE
matrixFile= open(fileName, "w+")
matrixFile.write("text: \n")
#matrixFile.write(str(distanceMatrix))
print("wrote to: ",fileName, file=sys.stderr)
matrixFile.close()
#READ <- Reads nothing right after writing to the fil
matrixFile= open(fileName, "w+")
print("About to read: ",fileName, file=sys.stderr)
print(matrixFile.read(), file=sys.stderr)
print("Read from: ",fileName, file=sys.stderr)
matrixFile.close()
except: #<- this except never triggers, which tells me the file is found, it's just to doing anything with it
print("cant open file", file=sys.stderr)