Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose v3: The difference between volume type mount and bind

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 type volume or bind

but never fully explains the difference. What is it?

like image 729
Kaleidophon Avatar asked May 31 '17 16:05

Kaleidophon


People also ask

What is the main difference between a volume mount and a bind mount?

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 bind mount a volume in docker?

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.

What is the difference between volume and volume mount?

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.

How many types of volumes are there in docker?

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.


1 Answers

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:
like image 95
Anoop Avatar answered Sep 20 '22 09:09

Anoop