Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Docker Volumes & Data Management
39%

Creating and Using Docker Volumes

Volumes are usually created and managed by Docker (but you can also create them manually). When you create a volume, Docker creates a directory under /var/lib/docker/volumes/ on the host machine and mounts it inside the container under the mount point:

/var/lib/docker/volumes//_data

Let's see how to create a volume:

docker volume create my-vol

This command will create a volume called my-vol and its folder is /var/lib/docker/volumes/my-vol/_data. For now, the folder should be empty:

ls -l /var/lib/docker/volumes/my-vol/_data

In order to use this volume in a real application, we need to mount it inside the container. We can do this using the -v flag:

docker run --name postgres-with-vol \
    -v my-vol:/var/lib/postgresql/data \
    -e POSTGRES_PASSWORD=mysecretpassword -d \
    postgres:16

When we write to the database, the data will be stored in the volume my-vol:

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More