Persistent volumes

Intermediate Docker

Mike Metzger

Data Engineering Consultant

What is a volume?

  • Volumes are an option to store data in Docker, unrelated to the container image or host filesystem
  • Are managed from the command line (or API)
  • Can share with multiple containers
  • Higher performance than file share / bind mounts
  • Exist until removed

volume mount.png

1 Image modified from https://docs.docker.com
Intermediate Docker

Managing volumes

  • docker volume
  • docker volume create <volumename>
  • docker volume ls or docker volume list
  • docker volume inspect
    • Provides assorted metadata about the volume, including Name, Mountpoint, Options, and so forth
  • docker volume rm
Intermediate Docker

Volume creation example

bash> docker volume create sqldata
sqldata
bash> docker volume ls
DRIVER    VOLUME NAME
local     2f2b7f710551e004dcdd9edf4cad31c37826b428de12f1c04ca02305d216ab00
local     14da7ff0c6eb29f644e6f9f9d59bbcf56b3699c04881dd7cbcaa9ecd6bef239c
local     150aa3c5c7aee30ffd1ec7ecf39f03989bf561536a9413ebed96ffbaa537d103
local     sqldata
...
Intermediate Docker

Volume inspect example

bash> volume inspect sqldata

[ { "CreatedAt": "2024-01-27T04:27:51Z", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/sqldata/_data", "Name": "sqldata", "Options": null, "Scope": "local" } ]
Intermediate Docker

Attaching volumes

  • Uses the -v command
    • docker run -v <volumename>:<destination path>:<options>
    • Volume name is name of existing volume
    • Destination path is the location the volume will be mounted (such as /data)
    • Options are optional comma-separated list of values such as ro for read-only.
  • --mount exists as with bind-mounts
$ docker run -v sqldata:/data postgres
Intermediate Docker

Drivers

  • Methods of storing Docker volumes
  • Can include:
    • Local filesystem (default)
    • NFS (Unix filesharing)
    • SMB / CIFS (Windows filesharing)
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...