Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t使用Flask和Docker卷写入文件_Docker_Flask - Fatal编程技术网

Can';t使用Flask和Docker卷写入文件

Can';t使用Flask和Docker卷写入文件,docker,flask,Docker,Flask,我正试图用Flask和docker写入一个文件。 如果在运行之前文件中有任何内容,则该文件将被清空并用空白页覆盖。但我的烧瓶应用程序实际上无法写入它 我认为我的卷设置不正确。以下是我所拥有的: 在我的道路上,我有: 矩阵/sampleMatrix.txt 静止的/ 模板/ app.py Dockerfile docker-compose.yml version: '2' services: <container name>: build: . ports:

我正试图用Flask和docker写入一个文件。 如果在运行之前文件中有任何内容,则该文件将被清空并用空白页覆盖。但我的烧瓶应用程序实际上无法写入它

我认为我的卷设置不正确。以下是我所拥有的:

在我的道路上,我有:

矩阵/sampleMatrix.txt 静止的/ 模板/ app.py Dockerfile docker-compose.yml

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)