Working with Docker Volumes

Docker Volumes are file systems that can be mounted on Docker containers to preserve data independent of the container lifecycle. They allow developers to backup data and share file systems among Docker containers easily. Multiple containers can mount the same volume, enabling seamless data sharing through simple commands and flags.

In this article, we will discuss how to create, list, inspect, mount, and delete Docker volumes using command-line operations.

Creating a Docker Volume

You can create a Docker volume using the create command. Docker creates a directory for the volume on the local machine at /var/lib/docker/volumes/.

sudo docker volume create <volume_name>

For example, to create a volume named myVolume

sudo docker volume create myVolume

Listing Docker Volumes

To list all existing Docker volumes, use the following command −

sudo docker volume list

This displays a list containing the driver name and volume name of all existing volumes.

Inspecting a Docker Volume

To inspect a Docker volume and view detailed information including creation date, mountpoint, driver name, and volume name, use −

sudo docker volume inspect <volume_name>

Mounting Docker Volumes

You can mount a Docker volume to a container using the --mount flag with the docker run command. The same volume can be mounted to multiple containers for shared access.

sudo docker run --mount source=<volume_name>,destination=<container_path> <image_name>

For example, to mount myVolume to an Ubuntu container at /usr/src/app/

sudo docker run -it --mount source=myVolume,destination=/usr/src/app/ ubuntu

This opens an Ubuntu bash with the volume mounted at the specified location.

Deleting Docker Volumes

Before deleting a volume, ensure it is not in use. Stop any containers using the volume, then remove it using −

sudo docker volume rm <volume_name>

To delete all unused volumes at once −

sudo docker volume prune

Sharing Volumes Between Multiple Containers

Docker volumes enable file sharing between multiple containers. Here's a practical example −

Step 1: Create and Mount Volume to First Container

sudo docker volume create myVolume
sudo docker run -it --name=container1 --mount source=myVolume,destination=/app ubuntu

Step 2: Create Files in the Volume

Inside the container bash, navigate to the mounted directory and create a file −

ls
cd app
touch tutorialspoint.txt
exit

Step 3: Mount Same Volume to Second Container

sudo docker run -it --name=container2 --mount source=myVolume,destination=/app ubuntu

In container2, navigate to /app and verify that tutorialspoint.txt exists. Any changes made to files in the volume will be reflected across all containers sharing it.

Common Volume Operations

Operation Command Purpose
Create docker volume create <name> Create new volume
List docker volume ls Show all volumes
Inspect docker volume inspect <name> View volume details
Remove docker volume rm <name> Delete specific volume
Cleanup docker volume prune Remove unused volumes

Conclusion

Docker volumes provide persistent storage that survives container restarts and deletions. They enable efficient data sharing between containers and simplify backup operations. Understanding volume management commands is essential for effective containerized application development.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements