Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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
Docker未保存图像并写入文本文件(Python应用程序)_Python_Docker_Docker Compose_Dockerfile_Devops - Fatal编程技术网

Docker未保存图像并写入文本文件(Python应用程序)

Docker未保存图像并写入文本文件(Python应用程序),python,docker,docker-compose,dockerfile,devops,Python,Docker,Docker Compose,Dockerfile,Devops,我创建了一个Python应用程序,它从电子邮件中接收图像,并将其本地存储在名为images的文件夹下。它还将一些配置数据写入dict.txt。当我在没有Docker的情况下运行应用程序(使用Pycharm IDE)时,这是可行的。然而,当我使用docker时,这种预期的行为不会发生 目录结构(容器): IntermediateService ├─── Dockerfile ├─── requirements.txt └─── src └─── emailAccountA.py

我创建了一个Python应用程序,它从电子邮件中接收图像,并将其本地存储在名为images的文件夹下。它还将一些配置数据写入dict.txt。当我在没有Docker的情况下运行应用程序(使用Pycharm IDE)时,这是可行的。然而,当我使用docker时,这种预期的行为不会发生

目录结构(容器):

IntermediateService
├─── Dockerfile
├─── requirements.txt
└─── src
     └─── emailAccountA.py
     └─── emailAccountB.py
     └─── main.py
     └─── dict.txt
     └─── Images
# set base image (host OS)
FROM python:3.8.5

# set the working directory in the container
WORKDIR /code

# copy the dependencies file to the working directory
COPY requirements.txt .

# install dependencies
RUN pip --default-timeout=100000 install -r requirements.txt

# copy the content of the local src directory to the working directory
COPY src/ .

# command to run on container start
CMD [ "python", "./main.py" ]
docker build -t intermediateservice .
docker run intermediateservice
Dockerfile:

IntermediateService
├─── Dockerfile
├─── requirements.txt
└─── src
     └─── emailAccountA.py
     └─── emailAccountB.py
     └─── main.py
     └─── dict.txt
     └─── Images
# set base image (host OS)
FROM python:3.8.5

# set the working directory in the container
WORKDIR /code

# copy the dependencies file to the working directory
COPY requirements.txt .

# install dependencies
RUN pip --default-timeout=100000 install -r requirements.txt

# copy the content of the local src directory to the working directory
COPY src/ .

# command to run on container start
CMD [ "python", "./main.py" ]
docker build -t intermediateservice .
docker run intermediateservice
我运行以下docker命令:

IntermediateService
├─── Dockerfile
├─── requirements.txt
└─── src
     └─── emailAccountA.py
     └─── emailAccountB.py
     └─── main.py
     └─── dict.txt
     └─── Images
# set base image (host OS)
FROM python:3.8.5

# set the working directory in the container
WORKDIR /code

# copy the dependencies file to the working directory
COPY requirements.txt .

# install dependencies
RUN pip --default-timeout=100000 install -r requirements.txt

# copy the content of the local src directory to the working directory
COPY src/ .

# command to run on container start
CMD [ "python", "./main.py" ]
docker build -t intermediateservice .
docker run intermediateservice

容器的构建和运行没有任何问题,只是不写入dict.txt,并将下载的图像保存到图像文件夹中(文本文件和文件夹都始终为空)。需要做些什么来解决这个问题?

正如@john gordon所说,它会写入容器内的文件系统,当容器退出时,文件系统就会丢失。运行文件夹时,需要将其装入容器中:

docker run-v$(pwd)/Images:/code/Images中间服务

正如@john gordon所说,它会写入容器内的文件系统,当容器退出时,文件系统就会丢失。运行文件夹时,需要将其装入容器中:

docker run-v$(pwd)/Images:/code/Images中间服务

容器是无状态的,因此当容器停止时,所有数据都会丢失。此外,在容器中写入文件也不是很实用

在这种情况下,最好在启动
docker run
时将主机文件夹装载到容器中。例如,这会将
images
子目录装入容器的
/code/images
目录:

-v $PWD/images:/code/images
$PWD
在Windows上无效,因此需要完整路径,例如
/c/myproject/images/


我写了一本书籍和视频课程,因为我找不到好的初学者教程来演示如何创建本地开发环境。这会有帮助的。黑色星期五减价50%。

容器是无状态的,因此当容器停止时,所有数据都会丢失。此外,在容器中写入文件也不是很实用

在这种情况下,最好在启动
docker run
时将主机文件夹装载到容器中。例如,这会将
images
子目录装入容器的
/code/images
目录:

-v $PWD/images:/code/images
$PWD
在Windows上无效,因此需要完整路径,例如
/c/myproject/images/


我写了一本书籍和视频课程,因为我找不到好的初学者教程来演示如何创建本地开发环境。这会有帮助的。黑色星期五减价50%。

它写入容器内的文件系统,当容器退出时文件系统丢失。@JohnGordon,谢谢。需要对dockerfile或docker命令进行哪些修改?它会写入容器内的文件系统,当容器退出时,文件系统将丢失。@JohnGordon,谢谢。需要对dockerfile或docker命令进行哪些修改?