Docker Compose: 哪种语法产生绑定挂载,绑定挂载产生卷

Docker Compose: Which syntax produces a bind mount, which produces a volume

在 Docker Compose 文档 here 中,您有以下与 docker-compose.yml 文件的 volumes 部分相关的示例:

volumes:
  # (1) Just specify a path and let the Engine create a volume
  - /var/lib/mysql

  # (2) Specify an absolute path mapping
  - /opt/data:/var/lib/mysql

  # (3) Path on the host, relative to the Compose file
  - ./cache:/tmp/cache

  # (4) User-relative path
  - ~/configs:/etc/configs/:ro

  # (5) Named volume
  - datavolume:/var/lib/mysql

哪些语法会产生 bind mount,哪些会产生 docker volume? 在文档的某些地方,这两个概念是严格区分的,但在这个地方它们混合在一起......所以我不清楚。

每当您在评论中看到“音量”时,就会 create a volume:因此 (1) 和 (5)。

如果评论里没有卷的话,这是约a bind mount

documentation regarding volumes in docker-compose is here:

Mount host paths or named volumes, specified as sub-options to a service.

You can mount a host path as part of a definition for a single service, and there is no need to define it in the top level volumes key.

But, if you want to reuse a volume across multiple services, then define a named volume in the top-level volumes key.

The top-level volumes key defines a named volume and references it from each service’s volumes list. This replaces volumes_from in earlier versions of the Compose file format. See Use volumes and Volume Plugins for general information on volumes.

这是两个完全不同的概念。卷意味着给定目录将在容器运行之间保留。想象一下 MySQL 数据库。您不想丢失数据。另一方面,有一个绑定挂载,您可以将本地目录附加到容器中的目录。如果容器在那里写了一些东西,它将出现在你的文件系统中,反之亦然(同步)。

附带说明,卷只不过是指向您计算机上目录的符号链接 :)(默认指向 /var/lib/docker/volumes/... 目录)