【问题标题】:Sharing a file between a persistent and a non persistent Docker Container在持久性和非持久性 Docker 容器之间共享文件
【发布时间】:2022-01-11 18:57:45
【问题描述】:

我正在使用Caprover 和 Docker 来运行非持久性的 Django 项目 (Django-Cookiecutter)。我还在导入 csv 数据的同一台服务器上运行持久应用程序。我需要这两个应用程序基本上一起共享一个文件。这是我想象中的样子:

  1. 导入器应用导入 csv 数据并将其作为 json 文件存储在特定文件夹中。
  2. Django 应用程序访问该文件夹中的 json 文件并使用我编写的脚本将 json 导入数据库。 我很难理解如何访问我的导入器应用程序中包含来自我的 django 应用程序的 json 文件的文件夹。有没有人知道如何在这两个 docker 容器之间实现这一点?

【问题讨论】:

  • 导入器应用程序是否可以接受 HTTP POST 请求,并直接写入数据库,而不涉及进程任一端的文件?在容器中设置会容易得多。
  • 我喜欢你的想法,但这是不可能的。用 Go 编写的导入器只允许创建 json 文件。

标签: docker docker-compose cookiecutter-django caprover


【解决方案1】:

您可以通过mounting 两个容器中的同一目录在两个容器之间共享一个目录或文件。这是一个简单的 docker-compose 示例,其中两个容器挂载同一个目录。然后第一个容器写入文件,第二个容器读取文件。

示例

version: '3'

services:
  writer:
    image: alpine
    command:
      [
        "sh",
        "-c",
        "echo hello from writer >/shared-writer/hello.txt"
      ]
    volumes:
      - .:/shared-writer
  reader:
    image: alpine
    depends_on:
      - writer
    command: [ "cat", "/shared-reader/hello.txt" ]
    volumes:
      - .:/shared-reader

注意:

  • 它们都挂载当前目录。
  • 第一个容器写入已安装卷内的文件。
  • 第二个容器从已安装卷内的同一文件中读取。
  • 容器使用不同的挂载点(/shared-writer/shared-reader)来证明主机中的同一个目录可以挂载到容器的不同位置。

输出

$ docker-compose up
Starting shared-mount-example_writer_1 ... done
Starting shared-mount-example_reader_1 ... done
Attaching to shared-mount-example_writer_1, shared-mount-example_reader_1
reader_1  | hello from writer
shared-mount-example_writer_1 exited with code 0
shared-mount-example_reader_1 exited with code 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多