容器不将数据写入物理磁盘文件
Container not writing data to physical disk file
Python代码:
import datetime,os
data = ""
with open("D://PythonService//Test.txt", "w") as outFile:
outFile.write(data + "Service started at - {}\n".format(datetime.datetime.now()))
outFile.close()
Docker 文件
FROM python:latest
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
CMD ["python", "WriteData.py"]
docker-compose.yml
version: '3.4'
services:
newfoldercopy:
image: newfoldercopy
build:
context: .
dockerfile: ./Dockerfile
volumes:
- D:/PythonService:/var/lib/data/
我正在尝试将我的物理文件系统路径挂载到容器。
但是容器不是在物理文件上写数据,容器是在容器虚拟文件上写数据。
据我了解
D:/PythonService
- 本地路径
/var/lib/data/
- 容器内的路径
在 Python 代码中,您应该使用容器路径 (/var/lib/data/) 而不是本地路径 (D:/PythonService)
您的代码应该类似于 with open("/var/lib/data/Test.txt", "w") as outFile:
在容器内的 /var/lib/data
文件夹中写入的所有文件将可供 D:/PythonService
使用
volumes:
- <host directory>:<container directory>
变化:
with open("D://PythonService//Test.txt", "w") as outFile:
至:
with open("/var/lib/data/Test.txt", "w") as outFile:
因为你的这一行 docker-compose.yml
:
volumes:
- D:/PythonService:/var/lib/data/
也代替:
outFile.write(data + "Service started at - {}\n".format(datetime.datetime.now()))
您可以使用 f-string
:
outFile.write(f'{data} Service started at - {datetime.datetime.now()}\n')
这使您的代码更具可读性。
据我了解,使用 with open()
会自动关闭您的文件,因此您可以更改 Python 代码:
with open("D://PythonService//Test.txt", "w") as outFile:
outFile.write(data + "Service started at - {}\n".format(datetime.datetime.now()))
outFile.close()
至:
with open("/var/lib/data/Test.txt", "w") as outFile:
outFile.write(f'{data} Service started at - {datetime.datetime.now()}\n')
Python代码:
import datetime,os
data = ""
with open("D://PythonService//Test.txt", "w") as outFile:
outFile.write(data + "Service started at - {}\n".format(datetime.datetime.now()))
outFile.close()
Docker 文件
FROM python:latest
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
CMD ["python", "WriteData.py"]
docker-compose.yml
version: '3.4'
services:
newfoldercopy:
image: newfoldercopy
build:
context: .
dockerfile: ./Dockerfile
volumes:
- D:/PythonService:/var/lib/data/
我正在尝试将我的物理文件系统路径挂载到容器。 但是容器不是在物理文件上写数据,容器是在容器虚拟文件上写数据。
据我了解
D:/PythonService
- 本地路径
/var/lib/data/
- 容器内的路径
在 Python 代码中,您应该使用容器路径 (/var/lib/data/) 而不是本地路径 (D:/PythonService)
您的代码应该类似于 with open("/var/lib/data/Test.txt", "w") as outFile:
在容器内的 /var/lib/data
文件夹中写入的所有文件将可供 D:/PythonService
volumes:
- <host directory>:<container directory>
变化:
with open("D://PythonService//Test.txt", "w") as outFile:
至:
with open("/var/lib/data/Test.txt", "w") as outFile:
因为你的这一行 docker-compose.yml
:
volumes:
- D:/PythonService:/var/lib/data/
也代替:
outFile.write(data + "Service started at - {}\n".format(datetime.datetime.now()))
您可以使用 f-string
:
outFile.write(f'{data} Service started at - {datetime.datetime.now()}\n')
这使您的代码更具可读性。
据我了解,使用 with open()
会自动关闭您的文件,因此您可以更改 Python 代码:
with open("D://PythonService//Test.txt", "w") as outFile:
outFile.write(data + "Service started at - {}\n".format(datetime.datetime.now()))
outFile.close()
至:
with open("/var/lib/data/Test.txt", "w") as outFile:
outFile.write(f'{data} Service started at - {datetime.datetime.now()}\n')