Docker volumes

Docker volumes are widely used and useful tools for ensuring data persistence while working in containers. They are a better alternative than compiling additional writable layers, which increase Docker image size.

Docker has two options for containers to store files in the host machine so that the files are persisted even after the container stops:
*Volumes are stored in a part of the host filesystem, which is managed by
*Bind mounts may be stored anywhere on the host system.

The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.

There are different ways to mount a Docker volume while launching a container. Users can decide between the -v and the --mount flags, which are added to the docker run command.

Create and Manage Volumes
The following command to create and manage Docker volumes outside the scope of any container.
#docker volume create [volume_name]
#docker volume create data
Docker automatically creates a directory for the volume on the host under the /var/lib/docker/volume/path. Mount this volume on a container, ensuring data persistence and data sharing among multiple containers.

List Docker Volumes
Use the following command to list the volumes.
# docker volume list
local data
The output displays a list of volumes, specifying their location and their volume name.

Inspecting Docker Volumes
Use the following command to inspect a volume.
#docker volume inspect [volume_name]
# docker volume inspect data
"CreatedAt": "2021-04-29T03:32:57-04:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/data/_data",
"Name": "data",
"Options": {},
"Scope": "local"
It lists all the details of a volume, including its location on the host file (mountpoint), and everything stored within the data volume can also be found in the directory listed under the mountpoint path.

Mounting a Data Volume
To mount a data volume to a container adds the --mount flag in the docker run command. It adds the volume to the specified container, storing the data produced inside the virtual environment. Use the following syntax to run a container and mount a data volume to it.
#docker run --mount source=[volume_name],destination=[path_in_container] [docker_image]

To launch an Ubuntu container and mount the data volume to it, run:
# docker run -it --name=example1 --mount source=data,destination=/data ubuntu
check to verify the volume was successfully mounted by listing the content of the container.

Copying Files Between Containers From a Shared Volume
*Once you have switched to the container command prompt, move to the data volume directory:
# cd data/

*Create an empty sample file using the command:
# touch sample1.txt

*Exit the container:
# exit

*launch a new container example2 with the same data volume:
# docker run -it --name=example2 --mount source=data,destination=/data ubuntu

*List the content of the container
data dev

*Move to the data directory and list the content of it
# cd data/

The output should list the sample1.txt file you created in the previous container (example1)
# ls

Remove a volume
To delete a Docker volume, we need to specify its name. Use the following basic command syntax to remove a volume
#docker volume rm [volume_name]
#docker volume rm data
Docker removes volumes only if they are not in use at the moment. If there is a container with the specified volume, it responds with an error.

Delete All Volumes at Once
Command to delete all unused Docker volumes at once
#docker volume prune
The output warns once that it will remove all local volumes not used by at least one container, and then we need to confirm to continue.

Recent Comments

No comments

Leave a Comment