# How the volumes are used in Docker

In 
Published 2022-12-03

This tutorial explains how the volumes could be used in Docker.

# Short explanation

Here are some points to underline:

  • by default data in Docker containers is not persistent (the data doesn’t persist when that container no longer exists)
  • in order to persist that data we need to store files on the host machine. We have 2 options here:
    • volumes
    • bind mounts

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.

bind mounts :

  • May be stored anywhere on the host system.
  • They may even be important system files or directories.
  • Non-Docker processes on the Docker host or a Docker container can modify them at any time.

# Docker volume usage

The following example are using the Docker volume created in How we can manage volumes in Docker tutorial.

Start a container with a volume:

docker run -d --name my-app-container -v docker-vol1:/app httpd:latest

The same thing could be done by using mount instead v:

docker run -d --name my-app-container --mount source=docker-vol1,target=/app httpd:latest

If we are using Docker Desktop we can see the containers which use a specific volume and the content of each volume.

Open Docker Desktop and click on "Volumes" and you will see something like this:

Click on a volume which is in use and you will see the containers which use this volume:

Click on "Data" and you will se the files we have into that volume.

From the container we can see the same files: