简体   繁体   English

在部分中和服务下声明docker-compose.yml卷有什么区别?

[英]What's the difference between declaring in docker-compose.yml volume as section and under a service?

What's the difference between declaring in the docker-compose.yml file a volume section and just using the volumes keyword under a service? docker-compose.yml文件中声明卷部分与仅在服务下使用volumes关键字有什么区别?

For example, I map a volume this way for a container: 例如,我以此方式为容器映射一个卷:

services:
   mysqldb:
      volumes:
         - ./data:/var/lib/mysql

This will map to the folder called data from my working directory. 这将映射到我的工作目录中名为data的文件夹。

But I could also map a volume by declaring a volume section and use its alias for the container: 但是我也可以通过声明一个卷部分并将其别名用作容器来映射一个卷:

services:
   mysqldb:
      volumes:
         - data_volume:/var/lib/mysql
volumes:
   data_volume:
      driver: local

In this method, the actual location of where the mapped files are stored appears to be somewhat managed by docker compose. 在这种方法中,存储映射文件的实际位置似乎由docker compose进行了某种程度的管理。

What are the differences between these 2 methods or are they the same? 这两种方法之间有什么区别或相同? Which one should I really use? 我应该真正使用哪一个?

Are there any benefits of using one method over the other? 使用一种方法相对于另一种方法有什么好处?

The difference between the methods you've described is that first method is a bind mount , and the other is a volume . 您所描述的方法之间的区别在于,第一个方法是绑定安装 ,另一个是 These are more of Docker functions (rather than Docker Compose), and there are several benefits volumes provide over mounting a path from your host's filesystem. 这些是更多的Docker函数(而不是Docker Compose),并且从挂载主机文件系统的路径中,卷提供了许多好处。 As described in the documentation, they: 如文档中所述,他们:

  • are easier to back up or migrate 更容易备份或迁移
  • can be managed with docker volumes or the API (as opposed to the raw filesystem) 可以使用docker volumes或API(而不是原始文件系统)进行管理
  • work on both Linux and Windows containers 在Linux和Windows容器上均可工作
  • can be safely shared among multiple containers 可以在多个容器之间安全共享
  • can have content pre-populated by a container (with bind mounts sometimes you have to copy data out, then restart the container) 可以由容器预先填充内容(使用绑定挂载,有时您必须将数据复制出来,然后重新启动容器)

Another massive benefit to using volumes are the volume drivers , which you'd specify in place of local . 使用卷的另一个巨大好处是卷驱动器 ,您可以在local 驱动器中指定它。 They allow you to store volumes remotely (ie cloud, etc) or add other features like encryption. 它们允许您远程存储卷(即云等)或添加其他功能(如加密)。 This is core to the concept of containers, because if the running container is stateless and uses remote volumes, then you can move the container across hosts and it can be run without being reconfigured. 这是容器概念的核心,因为如果正在运行的容器是无状态的并且使用远程卷,则可以在主机之间移动该容器,并且可以在不重新配置的情况下运行它。

Therefore, the recommendation is to use Docker volumes. 因此,建议使用Docker卷。 Another good example is the following: 另一个很好的例子如下:

services:
  webserver_a:
    volumes:
      - ./serving/prod:/var/www
  webserver_b:
    volumes:
      - ./serving/prod:/var/www
  cache_server:
    volumes:
      - ./serving/prod:/cache_root

If you move the ./serving directory somewhere else, the bind mount breaks because it's a relative path. 如果将./serving目录移动到其他位置,则绑定装入会中断,因为它是相对路径。 As you noted, volumes have aliases and have their path managed by Docker, so: 如您所述,卷具有别名,并且其路径由Docker管理,因此:

  1. you wouldn't need to find and replace the path 3 times 您无需查找并替换路径3次
  2. the volume using local stores data somewhere else on your system and would continue mounting just fine 使用local将卷存储在系统上其他位置的数据,并且可以继续安装

TL;DR: try and use volumes. TL; DR:尝试使用卷。 They're portable, and encourage practices that reduce dependencies on your host machine. 它们是可移植的,并鼓励减少对主机的依赖性的做法。

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

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