首页 > 解决方案 > Docker 不保存图像并写入文本文件(Python 应用程序)

问题描述

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

ImageLink:目录结构(Pycharm IDE)

目录结构(容器):

IntermediateService
├─── Dockerfile
├─── requirements.txt
└─── src
     └─── emailAccountA.py
     └─── emailAccountB.py
     └─── main.py
     └─── dict.txt
     └─── Images

Dockerfile:

# 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 命令:

docker build -t intermediateservice .
docker run intermediateservice

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

标签: pythondockerdocker-composedockerfiledevops

解决方案


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

docker run -v $(pwd)/Images:/code/Images intermediateservice

推荐阅读