docker compose volume type - bind vs volume

Efrat Levitan picture Efrat Levitan · Mar 26, 2019 · Viewed 23.3k times · Source

TLDR

In docker-compose, whats the difference between

volumes:
    - type: volume
      source: mydata
      target: /data

and

volumes:
    - type: bind
      source: mydata
      target: /data

?

The question in long:

When you specify the volumes option in your docker-compose file, you can use the long-syntax style

According to the docs, the type option accepts 3 different values: volume, bind and tmpfs:

I understand the tmpfs option - it means that the volume will not be saved after the container is down..

But I fail to find any reference in the docs about the difference between the other 2 options: bind and volume, could someone enlighten me about that?

Answer

β.εηοιτ.βε picture β.εηοιτ.βε · Mar 26, 2019

When bind mounts are files coming from your host machine, volumes are something more like the of docker.

  • Bind mounts are files mounted from your host machine (the one that runs your docker daemon) onto your container.
  • Volumes are like storage spaces totally managed by docker.
    You will find, in the literature, two types of volumes:
    • named volumes (you provide the name of it)
    • anonymous volumes (usual UUID names from docker, like you can find them on container or untagged images)

Those volumes come with their own set of docker commands; you can also consult this list via

docker volume --help

You can see your existing volumes via

docker volume ls

You can create a named volume via

docker volume create my_named_volume

But you can also create a volume via a docker-compose file

version: "3.3"

services:
  mysql:
    image: mysql
    volumes:
      - type: volume
          source: db-data
          target: /var/lib/mysql/data

volumes:
  db-data:

Where this is the part saying please docker, mount me the volume named db-data on top of the container directory /var/lib/mysql/data

- type: volume
    source: db-data
    target: /var/lib/mysql/data

And this is the part saying to docker please create me a volume named db-data

volumes:
  db-data:

Docker documentation about the three mount types: