Skip to Content

Docker volumes and bind mounts

All you have to know about volumes and bind mounts in docker!

Share on:
docker volume bind mount

Storing persistent data in containers needs special storage. There is two options - volumes or bind mounts.

How to use them?

Volume vs bind mounts

Bind mounts:

  • rely on docker host directory structure
  • different behaviour for --mount and --volume parameters
  • cant’ be control by docker CLI
  • used mainly in development environments

Volumes:

  • can be control by docker CLI
  • works in Linux and Windows hosts
  • easier to backup because of central location /var/lib/docker/volumes
  • easier to store in cloud provider infrastructure
  • preferred method as docker documentation said
  • give possibility to be shared between cluster nodes

At newer versions of Docker preferred method for configuring is --mount parameter instead of --volume.

Volume usage

Create volume

[lukas@docker-host ~]$ docker volume create lukas-space
lukas-space

List volumes

[root@docker-host ~]## docker volume ls
DRIVER              VOLUME NAME
local               lukas-space

Check volume physical location

[lukas@docker-host ~]$ docker volume inspect lukas-space
[
    {
        "CreatedAt": "2020-04-22T14:06:33+02:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/lukas-space/_data",
        "Name": "lukas-space",
        "Options": {},
        "Scope": "local"
    }
]

Driver local means that we didn’t use any external driver to create volume, it resides on docker host local storage.
External drivers will be covered in another post.

At docker host from root:

[root@docker-host ~]## ls -lah /var/lib/docker/volumes/lukas-space/_data
total 0
drwxr-xr-x. 2 root root  6 Apr 22 14:06 .
drwxr-xr-x. 3 root root 19 Apr 22 14:06 ..

Start container with created volume

Parameter --mount has following options:

  • type - volume(default), bind mount or tmpfs
  • source - name of volume
  • target - location in container filesystem where volume should be mounted
  • optionally readonly option

Options have to be delimited by comma.

We want to mount lukas-space volume at /lukas-space directory in read-write mode:

[lukas@docker-host ~]$ docker run -it --name ubuntu-server --mount source=lukas-space,target=/lukas-space ubuntu bash

Important!

  • if we didn’t create volume earlier docker will create it for us with name given in source field of --mount parameter
  • volumes by default will not be deleted while we removing container
  • if volume should be deleted with container - add --rm option to docker run command
  • if container has got in target directory any files, this files will be copied into volume

Check if mounted directory exists:

root@7d7887833e93:/## ls -lah /lukas-space/
total 0
drwxr-xr-x.  2 root root  22 Apr 22 12:23 .
drwxr-xr-x. 22 root root 261 Apr 22 12:24 ..

Create test file:

root@be1874c33798:/## touch /lukas-space/testfile

Check from docker host level that file appears:

[root@docker-host ~]## ls -lah /var/lib/docker/volumes/lukas-space/_data
total 0
drwxr-xr-x. 2 root root 22 Apr 22 14:23 .
drwxr-xr-x. 3 root root 19 Apr 22 14:06 ..
-rw-r--r--. 1 root root  0 Apr 22 14:23 testfile

You can make from this location backup or restore of volume data.

Bind mount usage

Create directory to mount

[lukas@docker-host ~]$ mkdir bindmount

Create container with directory mounted:

[lukas@docker-host ~]$ docker run -it --name ubuntu-server --mount type=bind,source=/home/lukas/bindmount,target=/lukas-space ubuntu bash

root@41cd34d22b7d:/## ls -lah /lukas-space/
total 0
drwxrwxr-x.  2 1000 1000   6 Apr 22 13:26 .
drwxr-xr-x. 22 root root 261 Apr 22 13:27 ..

Important!

  • if we didn’t create directory on docker host earlier docker will not create it for us with --mount parameter, auto-creating is available only in older --volume
  • bind mounts by default will not be deleted while we removing container
  • if container has got in target directory any files, this files will NOT be copied into bind mount directory, bind directory will cover any files in target container directory