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?
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.
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.
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.
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.
When bind mounts are files coming from your host machine, volumes are something more like the nas of docker.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With