Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 647
jeromerg Avatar asked Feb 19 '18 20:02

jeromerg


People also ask

What is Docker bind mount a volume?

Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.

Which is the Docker syntax to locate a volume which is mounted to a container?

Using Docker's “volume create” command The docker volume create command will create a named volume. The name allows you to easily locate and assign Docker volumes to containers.

Which command will create a volume in Docker?

This included running the commands: docker volume create data. docker run -it --name=example1 --mount source=data,destination=/data ubuntu.

Does Docker compose create volumes?

We can also create a volume with Docker compose service or also specify existing volumes. For example, the following screenshot shows a 'docker-compose' file that creates a docker-compose service with a volume. As a result of the above command, a volume with the name 'myvolume' gets created.


2 Answers

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.

like image 55
VonC Avatar answered Sep 27 '22 18:09

VonC


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)

like image 36
emix Avatar answered Sep 27 '22 17:09

emix