Docker — bind mounts and volumes

If your application requires data persistence after the container have been deleted docker offer two options to store data, volumes and…

If your application requires data persistence after the container have been deleted docker offer two options to store data, volumes and bind mounts

Bind mount have limited functionality compared to volumes. Using bind volumes a file or a directory on host machine is mounted into a container

Volumes are the preferred way for storing persistent data generated and used by containers, volumes have several advantages over bind volumes

  • Are easier to backup or migrate than bind mounts.
  • Volumes can be managed with docker cli commands or with the docker API
  • Volumes can be shared by multiple containers
  • Data in volumes can be encrypted by the volume drivers
  • There are a lot of volume drivers that you can benefit from their functionality

How to create a bind mount

Lets create a directory that will use as mount and a file for testing

$ cd ~/ 
$ mkdir message 
$ echo Hello, world! > message/message.txt

Now we will create a simple container with busybox that will mount this directory and print the contents of message.txt

  • type parameter is bind because we use bind mounts
  • The source parameter is the full path of the directory on the host, in my case is /home/kpatronas/message, adjust to your environment.
  • The destination is the full path on the container for source to be mounted
  • The readonly parameter is what you guess, no write operations allowed
$ docker run --mount type=bind,source=/home/kpatronas/message,destination=/root,readonly busybox cat /root/message.txt

Running this command should print Hello, world!Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
e5d9363303dd: Pull complete
Digest: sha256:c5439d7db88ab5423999530349d327b04279ad3161d7596d2126dfb5b02bfd1f
Status: Downloaded newer image for busybox:latest
Hello, world!

A simpler form of the command is the followingdocker run -v /home/kpatronas/message:/root:ro busybox cat /root/message.txt

Parameters

  • - v: volume
  • source:destination source path to mounted to the destination path on the container
  • ro: read-only

To create a mounted volume

docker run --mount type=volume,source=test-volume,destination=/root busybox sh -c 'echo hello > /root/message.txt && cat /root/message.txt'

This command will type “hello” on the terminal

  • the type parameter is volume, because we use mounted volume
  • source is the name of the mounted volume
  • destination is the path on the container that the volume will mounted

A shorter form of the command is

docker run -v test-volume:/root busybox sh -c 'cat /root/message.txt'

Docker knows that test-volume is volume and not a bind mount because there is no / in.

How to administer volumes

List volumesdocker volume ls
DRIVER              VOLUME NAME
local               my-volume

Get details for a volume$ docker volume  inspect my-volume
[
   {
       "CreatedAt": "2021-01-17T22:52:27Z",
       "Driver": "local",
       "Labels": null,
       "Mountpoint": "/var/lib/docker/volumes/my-volume/_data",
       "Name": "my-volume",
       "Options": null,
       "Scope": "local"
   }
]

To create a volume$ docker volume create test

To delete a volume$ docker volume rm test

To delete an unused volume (warning use this command with special care)$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N]

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…