简体   繁体   English

Docker-数据量和数据容器之间的区别

[英]Docker - difference between data volumes and data containers

i do not understand what difference there is between data volume and data containers... For example this two docker-compose confguration have same result but the first use a data container 我不明白数据量和数据容器之间有什么区别...例如,这两个docker-compose配置具有相同的结果,但第一个使用数据容器

whit data container 数据容器

datacontainer:
 image: httpd:2.4-alpine
 volumes:
  - ../src:/usr/local/apache2/htdocs

apache:
 image: httpd:2.4-alpine
 ports:
  - 80:80
 volumes_from:
  - datacontainer

without data container 没有数据容器

apache:
 image: httpd:2.4-alpine
 ports:
  - 80:80
 volumes:
  - ../src:/usr/local/apache2/htdocs

what are the advantages and disadvantages of using a data container? 使用数据容器的优点和缺点是什么?

Neither one of those examples are really data volumes or data containers. 这些示例都不是真正的数据卷或数据容器。 They are both bind mounts to the host, one more indirectly than the other. 它们都绑定到主机,比另一个间接得多。 I tend to refer to those as host volumes. 我倾向于将这些称为主机卷。

A data container is deprecated to the named volumes. 数据容器已弃用到命名卷。 They would look like: 它们看起来像:

datacontainer:
 image: httpd:2.4-alpine
 command: tail -f /dev/null
 volumes:
  - /data

apache:
 image: httpd:2.4-alpine
 ports:
  - 80:80
 volumes_from:
  - datacontainer

Then you could recreate the apache container, or upgrade it, without upgrading the datacontainer, and still have your data available. 然后,您可以重新创建apache容器或对其进行升级,而无需升级datacontainer,仍然可以使用数据。 There are multiple drawbacks to this, and if implemented with the volume defined in the image I posted a blog about just how much I dislike that . 这样做有很多弊端,如果按照图片中定义的数量实施,我会在博客中发布有关我有多不满意的博客 But the biggest issue is that you're managing your data as a container, so any container cleanup routines will equally cleanup your data, not very good if your opposed to data loss or container sprawl. 但是最大的问题是,您将数据作为容器进行管理,因此任何容器清理例程都将同样地清理数据,如果您反对数据丢失或容器蔓延的话,效果会非常差。

A named volume is much easier and has all the same features of a data container, but separates the data management from the container management, allowing containers to be purged without fear, and data to be backed up from one place. 命名卷要容易得多,并且具有数据容器的所有相同功能,但是将数据管理与容器管理分离开,从而无需担心即可清除容器,并可以从一个位置备份数据。 They look like: 他们看着像是:

apache:
 image: httpd:2.4-alpine
 ports:
  - 80:80
 volumes:
  - data:/data

Note in version 2 of compose, you'd be more specific with the volume definition. 请注意,在compose的第2版中,您将更详细地定义卷。

The data container pattern was designed around data persistence, though it is mostly obsoleted by named volumes in Docker 1.9. 数据容器模式是围绕数据持久性设计的,尽管它在Docker 1.9中已被命名卷淘汰。 The main advantage of both volumes and the data container pattern is that bind mounting on a host is host dependent, meaning you couldn't use that in a docker file. 卷和数据容器模式的主要优点是,主机上的绑定安装取决于主机,这意味着您不能在docker文件中使用它。 Volumes allow you the flexibility to define volumes when you build your images. 卷使您可以灵活地在构建映像时定义卷。

In addition, Data volumes are designed to persist data, independent of the container's life cycle. 此外,数据卷旨在持久存储数据,而与容器的生命周期无关。 Docker therefore never automatically deletes volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container. 因此,Docker永远不会在您删除容器时自动删除卷,也不会“垃圾收集”不再由容器引用的卷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM