# How we can manage volumes in Docker

In 
Published 2022-12-03

This tutorial explains how we can manage a Docker volume.

# 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.

# Manage volumes

Create a Docker volume:

docker volume create docker-vol1

List volumes:

docker volume ls

Inspect a volume:

docker volume inspect docker-vol1

The result is in JSON format:

[
{
"CreatedAt": "2023-01-27T17:07:43Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/docker-vol1/_data",
"Name": "docker-vol1",
"Options": {},
"Scope": "local"
}
]

If you are using Docker Desktop, you can see it in "Volumes" screen:

You can see the status of each volume as well.

Remove a volume:

 docker volume rm docker-vol1

When you run a container, you can like a host folder to the container folder:

docker run -it --name bindmount --mount type=bind,source=c:\data1,target=c:\data2 microsoft/windowsservercore powershell