【问题标题】:Docker, Volumes vs Bind Mounts for persistent data such as DB, elasticsearch?Docker、Volumes vs Bind Mounts 用于持久数据,例如 DB、elasticsearch?
【发布时间】:2019-02-04 15:23:10
【问题描述】:

docker data volume vs mounted host directory

volumes 应该优先于bind mounts

我有几个关于这个问题的问题。帖子说:

When you create a volume, it is stored within a directory on the Docker host

请耐心等待,但我是 docker 新手,我想知道这里的 docker host 是什么。

它是我构建映像的机器吗(可能不是)?
是运行映像的机器吗?如果是这样,如果我在多台机器上运行映像会发生什么情况,它会创建两个独立的卷吗?
当我设置了developementproduction 时,docker 如何为每个环境管理两个单独的卷?

除了当我使用数据量时使用docker-compose down 似乎很容易丢失数据,这是让我犹豫使用data volumes 的第一个障碍,有没有明显的解决方案来缓解这个问题?

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    这实际上不是一个教义——不使用绑定坐骑。是的,如果默认情况下您在容器内拥有 root 权限,如果安装不准确(如 -v /bin:/var/log),它们可能会损坏您主机的文件系统;它们也不太便携,但它们促进了主机和容器之间的文件交换。当您想为您的服务提供初始配置,或将编译源代码放入容器中时,我相信您会更喜欢bind mount,而不是为docker volume cp 操作创建和运行临时容器。此外,您应该尽可能使用:ro 选项(只读),以防止从容器内部修改数据。

    Docker 主机 - 它是一台机器 (PC),运行 Docker 守护进程。

    它是我构建图像的机器吗(可能不是)?

    不正确。您可以使用docker CLIdocker API 远程构建。

    是运行镜像的机器吗?

    是的,图像由 docker daemon 运行,因此它将是主机。

    如果是这样,如果我在多台机器上运行映像会发生什么, 它会创建两个独立的卷吗?

    这取决于。在不同的机器上运行镜像可以通过不同的方式来实现,以 kubernetesdocker swarm 之类的协调器为目标,并以在单独的 docker 守护进程上手动启动结束。使用编排器可以有相同的卷,在不同的主机之间共享,但在这种情况下你不能使用bind mounts,你可以使用volumes

    当我进行开发和生产设置时,docker 如何管理两个 每个环境都有单独的卷?

    Docker 不是由你来管理的。

    此外,通过 docker-compose down 似乎很容易丢失数据 当我使用数据量时,这是让我 犹豫使用数据量,是否有明显的解决方案来缓解 问题?

    卷可以在docker-compose 会话之间轻松保留。实现这一点的最明确方法是提前声明音量

    docker volume create foo
    

    然后在您的撰写文件中使用它:

    version: '3'
    services:
      abc:
        volumes:
          foo:/foo
    volumes:
      foo:
        external: true
    

    【讨论】:

      【解决方案2】:
      Feature Bind Volume                                 
      Internal soul Bind mounts attach a user-specified location on host filesystem to a specific point in a container file tree. Volume attach with disk storage on the host filesystem or cloud storage.
      command --mount type=bind,src="",dst="" Docker CLI
      docker volume command
      Dependency dependent on location on to the host filesystem. Container-independent data management
      Separation of concerns No Yes
      Conflict with other containers Yes Example: multiple instances of Cassandra that all use the same host location as a bind mount for data storage. In that case, each of the instances would compete for the same set of files. Without other tools such as file locks, that would likely result in corruption of the database. No. By default, Docker creates volumes by using the local volume plugin.
      When to choose 1- Bind mounts are useful when the host provides a file or directory that is needed by a program running in a container, or when that containerized program produces a file or log that is processed by users or programs running outside containers.
      2- appropriate tools for workstations, machines with specialized concerns
      3- systems with more traditional configuration management tooling.
      Working with Persistent storage
      1. Databases
      2. Cloud storage
      When not to choose Better to avoid these kinds of specific bindings in generalized platforms or hardware pools. To be written

      【讨论】:

        猜你喜欢
        • 2022-10-21
        • 2020-06-03
        • 1970-01-01
        • 2018-12-14
        • 1970-01-01
        • 2017-10-19
        • 2020-08-11
        • 2020-04-06
        • 2017-01-03
        相关资源
        最近更新 更多