【问题标题】:Docker not saving images and writing to textfile (Python application)Docker 不保存图像并写入文本文件(Python 应用程序)
【发布时间】:2020-11-24 15:36:28
【问题描述】:

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

ImageLink: Directory structure (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 文件夹(文本文件和文件夹总是空的)。需要做什么来解决这个问题?

【问题讨论】:

  • 它写入文件系统inside容器,当容器退出时丢失。
  • @JohnGordon 谢谢。 dockerfile或者docker命令需要做哪些修改?

标签: python docker docker-compose dockerfile devops


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    -v $PWD/images:/code/images
    

    $PWD 在 Windows 上无效,因此您需要完整路径,例如 /c/myproject/images/)。

    我写了一本Docker for Web Developers 书籍和视频课程,因为我找不到适合初学者的很好的教程来展示如何创建本地开发环境。它会有所帮助。现在有 50% 的黑色星期五特卖。

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多