Volumes Docker

Use volumes

Estimated reading time: 12 minutes

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker.

Volumes have several advantages over bind mounts:

  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers.
  • Volume drivers allow you to store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
  • A new volume’s contents can be pre-populated by a container.

In addition, volumes are often a better choice than persisting data in a container’s writable layer, because using a volume does not increase the size of containers using it, and the volume’s contents exist outside the lifecycle of a given container.

Create and manage volumes

Unlike a bind mount, you can create and manage volumes outside the scope of any container.

docker volume create

Create a volume:

docker volume create my-vol

docker volume ls

Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\pipenv>docker volume ls
DRIVER              VOLUME NAME
local               03f1a1ed0555015e51863dbed5f6c7847099fd33449b9d83919bd7028cdfdd9b
local               4bc5fb631c6af81f5ba84a8465b3c2805ca713541fe736faf3a232ef4b24ae72
local               56295a3bb8a90d260864c258e6b174755338543a614f206e5082c066d22eb197
local               67871ba2f3b3a9e75fdbfcf2fe5ec36ba7a10cd5930a60e8227abc7110e62ca4
local               b6432532ff915143ede0b7169abf7690790ce0227277013d7c5ab00007d68703
local               bbef076d429a90ca7bfd7a751d7c8aa1ea3d08e0b7b4036bb296681545940a0b
local               bf69b1f1164c09d7dc0f3e6011f3116e7bc197e0e9341e645a15fdc7566489f3
local               cee0d9feda75150bda5e6b32c5eeaad4e433afe01165bf822eae8413b1f4e861
local               pgdata
local               postgresql_postgres_data
local               vote_db-data