I am using docker-compose
syntax version 3 and want to use some volumes. The documentation on the long syntax for volumes states the following:
type
: the mount typevolume
orbind
but never fully explains the difference. What is it?
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.
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.
A volume always keeps data in /var/lib/docker/volumes, while mount points can be created wherever we want. If a container which is assigned a mount point is also assigned a volume then all data from the mount point is copied to the volume automatically, while the opposite is not true.
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
is the simpler one to understand. It takes a host path, say /data
and mounts it inside your container, say /opt/app/data
. /data
can be anything, probably mounted on NFS
or it maybe a local host path.
docker run -v /data:/opt/app/data -d nginx
volume
mount is where you can use a named volume.
You would normally use a volume driver for this, but you can get a host mounted path using the default local volume driver something like the below:
docker volume create data
docker run -d -v data:/opt/app/data nginx
The named volume can also be anonymous if you run just this:
docker run -d -v /opt/app/data nginx
If you run docker volume ls
, docker would have create an autogenerated long name for the anonymous volume.
In docker-compose, you would just use it as below:
web:
image: nginx:latest
volumes:
/data:/opt/app/data
data:/opt/app/data1
volumes:
data:
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