To create and manage Docker volume, Docker offers various commands as creating Docker volume by utilizing the “docker volume create”, inspecting the volume through the “docker volume inspect” command, mounting the volume with the container through “–mount” or “-v” options, removing the Docker volume using “docker volume rm” command, and to prune or remove all dangling unused volume, utilize “docker volume prune” command.

Let’s understand what Docker volume is and then dive into the ways to create and manage it.

What is Docker Volume?

Docker volume is an external storage system that is used for persisting the container data. It is completely independent of the container’s life cycle which means if the container is terminated or removed, Docker volume still persists and saves the container’s data. Therefore, it is also used to create the container data’s backup. Additionally, the data saved in volume can be easily shared and used by other containers in the Docker host. The developers can use and manage the Docker volume through Docker CLI and the Docker APIs.

How to Manage Docker Volume?

Different Docker CLI commands are used to manage and operate the Docker volume with Docker containers. To get started with Docker volume, go through the listed procedure:

  • Create Docker Volume
  • List Docker Volume
  • Inspect Docker Volume
  • Mount Docker Volume
  • Backup and Restore Docker Data Volume
  • Remove Docker Volume
  • Prune Docker Volume

So, let’s get started!

Create Docker Volume

Volume is completely managed by the Docker application. It can be utilized for both Windows and Linux containers. To create a new volume in Docker, you can simply use the “docker volume create <volume name>” command:

docker volume create hands-on-vol

In the above output, we have created a “hands-on-vol” volume in Docker:

docker volume create hands-on-vol

List Docker Volume

To view or list down Docker volume, utilize the “docker volume ls” command:

docker volume ls

This will display all the volumes that exist in the Docker application and that are being used by any container:

docker volume ls

The above output shows that we have effectively created a new volume named “hands-on-vol”.

Inspect Docker Volume

Sometimes, users want to view the short summary of volume to check what is the volume mounted point and where data will be stored. For this purpose, they can inspect the Docker volume through the “docker volume inspect <volume name>” command:

docker volume inspect hands-on-vol
docker volume inspect hands-on-vol

Mount Docker Volume

To preserve the container data in the external file system, the user needs to mount the Docker volume with the container. To mount Docker volume, the following approaches can be utilized:

  • Mount Volume Through “–mount
  • Mount Volume Through “-v
  • Mount Volume Through Docker Compose

Approach 1: Mount Volume Through “–mount”

The “–mount” option can be used to mount the volume with the container while creating the Docker container either through the “docker run” command or through the “docker container create” command. To mount a volume with a container using the “–mount” option, go through the given steps:

Step 1: Create Docker Image

First, create the Docker image to containerize your application. For this purpose, you can follow our linked article “Create Docker Image| Beginner Guide”:

Step 2: Mount Volume While Executing Container

After that, create and start the Docker container and also mount the external file system (Docker volume) to save the data in external storage outside the container. For this purpose, utilize “docker run –mount source=<volume name>,target=<path to container> <docker image>” command:

docker run -d -p 80:80 --name html-cont --mount source=hands-on-vol,target=/usr/share/nginx/html html-img

In the above command, the following options are used to create the container:

  • “-d” is used to start the container execution in detached mode.
  • “-p” is used to publish the container port on the host system to access the application outside the container.
  • The “–name” option defines the container name.
  • The “–mount” option is used to mount the volume with the container. It uses two variables “source” and “target”. Here, “source” is used to specify the volume name, and the “target” option is used to define the path of the container where the source volume will be mounded:
docker run -d -p 80:80 --name html-cont --mount source=hands-on-vol,target=/usr/share/nginx/html html-img

For verification, list down the Docker containers:

docker ps
docker ps

To check if the container is published on the specified port, open the “http://localhost:80” URL in the browser and check if the application is accessible or not:

Docker demo application

Step 3: Inspect Container

For verification, inspect the Docker container through the “docker inspect <container name>” command:

docker inspect html-cont
docker inspect html-cont

Under the “Mounts” key, you can see we have effectively mounted the “hands-on-vol” on the container path “/usr/share/nginx/html”:

Output

Approach 2: Mount Volume Through “-v”

Another possible way to mount the Docker volume with the container is through the “-v” option in the “docker run” or “docker container create” command. For demonstration, follow the mentioned steps.

Step 1: Mount Volume Using “-v”

To mount Docker volume using the “-v” option, utilize the “docker run -v <volume name>:<path to container> <image name>” command:

docker run -d -p 80:80 --name html-cont-1 -v hands-on-vol:/usr/share/nginx/html html-img

In the above command, the source volume and target path are specified directly without using any variables. This time we have mounted the Docker volume with the “html-cont-1” container which means we can share a single volume with multiple containers:

docker run -d -p 80:80 --name html-cont-1 -v hands-on-vol:/usr/share/nginx/html html-img

Step 2: Verification

For confirmation, again inspect the container and check if the volume is mounted or not:

docker inspect html-cont-1
docker inspect html-cont-1

The below output indicates that we have effectively mounted the “hands-on-vol” and its default type is “volume”:

Output

Approach 3: Mount Volume Through Docker Compose

The Docker compose is a plugin and Docker CLI command that manages and executes multi-container services or apps. In order to mount a volume in Docker Compose, the “volumes” key is used. For illustration, check out the following procedure:

Step 1: Create “docker-compose.yml” File

First, create the file in the text editor named “docker-compose.yml” file:

sudo nano docker-compose.yml
sudo nano docker-compose.yml

Next, paste the given json code into the file:

version: "3"
services:
  web-page:
    image: html-img
    volumes:
      - hands-on-vol:/usr/share/nginx/html
    ports:
      - 80:80

volumes:
  hands-on-vol:
    external: true

In the mentioned code:

  • The “services” key specifies the service name running in the container.
  • The “image” key defines the container template through which the specified service will be containerized.
  • The “volumes” key is utilized to mount the Docker volume. Here source value will be mentioned on the left and the target container path is specified on the right side of the “:”.
  • The “ports” key is used to publish the container port on the host system to expose the service outside the container.
  • Lastly, the source volume is defined and set as an “external” entity.

Step 2: Start Compose Container

In the next step, fire up the service by starting the container through the “docker compose up”. The “-d” will run the service at backend (detached mode):

docker compose up -d
docker compose up -d

Step 3: Inspect the Container

For verification, inspect the container through the “docker inspect <container name>” command:

docker inspect hands-on-web-page-1
docker inspect hands-on-web-page-1

The below result indicates that we have mounted the external volume “hands-on-vol” successfully through Docker compose:

Output

Backup and Restore Docker  Data Volume

The Docker volume is also used to create the container data backup. So, if the container is deleted, the data will not be affected and can be reused with another container. To create the volume backup and to restore the volume from the generated backup, follow the below commands: 

Backup Data Volume

To create the backup of volume, run another container that will create the backup in the specified directory. For this purpose, check out the mentioned command:

docker run --rm --volumes-from html-cont -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /usr/share/nginx/html

In the given code block:

  • The “–rm” option is used to remove the container automatically after creating a backup.
  • The “–volumes-from” option is used to access the volume from another container to create the volume backup.
  • -v” is used to mount the system directory where data will be stored. Here, the “pwd” command is used to mount the current working directory to save the volume backup.
  • ubuntu” is a Docker image used to run the container.
  • The data will be accessed from the container directory where the volume is mounted and stored in “/backup/backup.tar”:
docker run --rm --volumes-from html-cont -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /usr/share/nginx/html

For confirmation, run the “ls” command and check if the volume backup is created or not:

ls

The output shows that we have effectively generated the backup of the “hands-on-vol” volume in the “backup.tar” file:

ls

Let’s restore the backup in another container.

Restore Data Volume

To restore the volume backup in another container or to share a similar volume between different containers, utilize the given command:

docker run --rm --volumes-from html-cont-1 -v $(pwd):/backup ubuntu bash -c "cd /usr/share/nginx/html && tar xvf /backup/backup.tar --strip 1"

In the above command, the backup file “/backup/backup.tar” will be restored in the “/usr/share/nginx/html” directory of the “html-cont-1” container:

docker run --rm --volumes-from html-cont-1 -v $(pwd):/backup ubuntu bash -c "cd /usr/share/nginx/html && tar xvf /backup/backup.tar --strip 1"

Remove Docker Volume

In order to remove the volume in Docker, simply use the “docker volume rm <volume name>” command:

docker volume rm hands-on-vol
docker volume rm hands-on-vol

Note: If the volume is being used by any container, the user will get an error “volume is in use – <container id>”. To avoid the error, try to unmount the volume from the container or first remove the container and then remove the Docker volume:

Error

Prune Docker Volume

To remove all volumes from Docker, simply run the “docker volume prune” command. Here, the “-a” flag is utilized to remove all volume. If the “-a” option is not used, it will only remove unused or dangling volumes:

docker volume prune -a

This operation will ask you for permission. Hit “Y” to continue. This will remove all volumes from Docker:

docker volume prune -a

For verification, list down the volumes and check whether these are removed or not:

docker volume ls
docker volume ls

That is all about managing and using volume in Docker.

Conclusion

Different commands are utilized to manage volume such as “docker volume create” creates the new volume, “docker volume inspect” inspects the volume, and “docker volume rm” removes the volume. To mount any volume with docker, the “-v” or “–mount” option is used in the “docker run” and “docker container create” commands. To create volume backup and restore volume in the container, the “–volume-from” option is used. This blog has explained in detail how to use and manage Docker volume.