9

In the Docker Compose documentation, here, you have the following example related to the volumes section of docker-compose.yml files:

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

Which syntaxes produce a bind mount and which produce a docker volume? At some place of the documentation, the two concepts are strictly differentiated but at this place they are mixed together... so it is not clear to me.

jeromerg
  • 2,997
  • 2
  • 26
  • 36
  • 1
    Given that a bind mount requires mounting a *specific* host directory, I'd wager that all of the examples that specify a host directory are bind mounts. – Oliver Charlesworth Feb 19 '18 at 20:34

2 Answers2

4

Whenever you see "volume" in the comment, that will create a volume: so (1) and (5).

If there is not a volume in the comment, this is about a bind mount.

https://docs.docker.com/storage/images/types-of-mounts-bind.png

The 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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thank you for clarifying the point! I indeed missed the fact that the word "volume" was used at point (1) and (5) but for the other points. Unfortunately at some places of the Docker API and documentation, the word "volume" is used for both "volume" and "bind mount" (i.e. the CLI option `-v` or compose file `-volumes` element are used to declare either volumes or bind mount)... so these kind of confusions don't help really to separate the two concepts... – jeromerg Feb 20 '18 at 21:17
  • 2
    The question about the differences in terms of the docker-compose syntax isn't answered yet, or? – Aydin K. Nov 21 '18 at 13:45
  • @AydinK. Indeed. I have added at least a reference to the docker-compose documentation. – VonC Nov 21 '18 at 14:13
2

Those are two completely different concepts. A volume means that given directory will be persisted between container runs. Imagine MySQL database. You don’t want to lose your data. On the other hand there’s a bind mount where you attach your local directory to the directory in the container. If the container writes something there it will appear in your file system and vice versa (synchronization).

As a side note a volume is nothing more than a symlink to the directory on your machine :) (to a /var/lib/docker/volumes/... directory by default)

Mike Doe
  • 16,349
  • 11
  • 65
  • 88