Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker compose volume type - bind vs volume

Tags:

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?

like image 617
Efrat Levitan Avatar asked Mar 26 '19 21:03

Efrat Levitan


People also ask

What is the difference between BIND and volume in Docker?

Getting started using bind mounts The most notable difference between the two options is that --mount is more verbose and explicit, whereas -v is more of a shorthand for --mount . It combines all the options you pass to --mount into one field.

What is volume binding in Docker?

This means that there is one behavior that is different between -v and --mount . If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory.

What are the two types of Docker volumes?

Docker volumes are used to persist data from within a Docker container. There are a few different types of Docker volumes: host, anonymous, and, named. Knowing what the difference is and when to use each type can be difficult, but hopefully, I can ease that pain here.

What is volume bind?

Bind mounts: A bind mount is a file or folder stored anywhere on the container host filesystem, mounted into a running container. The main difference a bind mount has from a volume is that since it can exist anywhere on the host filesystem, processes outside of Docker can also modify it.


1 Answers

When bind mounts are files coming from your host machine, volumes are something more like the nas 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:

  • https://docs.docker.com/storage/bind-mounts/
  • https://docs.docker.com/storage/volumes/
  • https://docs.docker.com/storage/tmpfs/
like image 63
β.εηοιτ.βε Avatar answered Sep 28 '22 09:09

β.εηοιτ.βε