Using volumes

that are managed by Docker

Using volumes is your best bet if you want to keep the container data outside the container.

Docker Docs: Volumes

Table of contents
  1. Using volumes
    1. Exercise

The advantage of volumes is, that they are managed by Docker itself and that they offer more functions and options then bind mounts.

The disadvantage of volumes is, that their content cannot be easily accessed from the host computer. This makes modifications and backups more complicated comparing to bind mounts.

They also don’t support binding of single files, only the whole volumes.

From Docker Docs
Volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker.

Because the Docker CLI functionality is evolving, it’s recommended to follow the section Volumes in the official Docker documentation.

Before persisting data outside the container you should read the section Manage data in Docker in the official Docker documentation.

Exercise

Let’s say that you want to persist the container’s folder /home/headless/Documents in the named Docker volume my-documents. If the volume does not exist yet, it should be created. Otherwise it should be re-used. You want to name the container devrun.

You can do it by creating the container using the following command:

docker run -d --name devrun -p "35901:5901" -p "36901:6901" -v "my-documents:/home/headless/Documents" accetto/ubuntu-vnc-xfce-g3

You can connect to the container as it’s described in the section Headless working and store some files into the folder /home/headless/Documents.

Then you can remove the container using the command:

docker container rm -f devrun

You can verify that the volume has not been removed with the container by using the following command:

docker volume ls

If you now re-create the container, it will use the same volume and all the data will be still there, until the volume is deleted.

You can delete the volume explicitly using the following command:

docker volume rm my-documents

Also the exercise Avoiding pitfall HOME binding uses Docker volumes.

Previous Top Next