#
How we can manage volumes in Docker
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"
}
]
Info
- My test is done on Windows + WSL 2 (Ubuntu). In order to see the content of the file on Windows I go to
\\wsl$\docker-desktop-data\data\docker\volumes\docker-vol1\_data
. - You can add files into this folder directly on Windows or through the container.
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
Info
In general, --mount
is more explicit and verbose. The biggest difference is that the -v
syntax
combines all the options together in one field, while the --mount
syntax separates them.