Home > Other External Packages > docker volume

Docker Volume Command Guide

Docker volumes are the recommended mechanism for persistently storing data generated and used by Docker containers. Even if a container is deleted, the data stored in the volume is preserved, allowing you to manage containers without data loss. This guide covers how to create, manage, and delete Docker volumes.

Overview of Docker Volumes

A Docker volume is a directory or file that is mounted to a specific path in the host's file system. It allows data to be managed independently of the container's lifecycle, which is more flexible and efficient than bundling data with the container image. Volumes can also be shared by multiple containers simultaneously.

Why Volumes Are Needed

Containers are ephemeral by nature. This means that when a container is deleted, all data stored inside it is also lost. To permanently preserve important data like databases, log files, or user-uploaded files, you must use volumes. Volumes solve this problem, enhancing the stability and flexibility of container-based applications.

Advantages of Volumes

The main benefits of using Docker volumes are as follows:

  • Data Persistence: Data is preserved even if the container is deleted.
  • Data Sharing: Multiple containers can share the same volume to access data.
  • Performance: It can provide better I/O performance than bind mounts.
  • Portability: Volumes are not included in the Dockerfile, allowing images to be kept lighter.
  • Backup/Restore: Volumes can be easily backed up and restored.

Key Docker Volume Commands

Let's explore various commands for efficiently managing Docker volumes. Try combining each option to create, inspect, delete, and connect volumes to containers.

1. Creating and Listing Volumes

2. Deleting Volumes

3. Connecting a Volume to a Container

Generated command:

Try combining the commands.

Description:

`docker volume` Executes the command.

Combine the above options to virtually execute commands with AI.

Types of Volumes

In Docker, there are two primary types of mounts to connect data to containers: Volumes and Bind mounts. Each has different use cases and characteristics.

1. Volumes

This is the most preferred method for persistently storing data in Docker. It is stored in a specific area of the host's file system managed by Docker.

  • Creation and Management: They are created and managed via the Docker CLI, using `docker volume` commands.
  • Location: Stored in the host's `/var/lib/docker/volumes/` path. It is not recommended for users to access this internal path directly.
  • Characteristics: They are independent of the container's lifecycle and can be shared across multiple containers. They offer performance benefits, especially in Docker Desktop.

2. Bind mounts

Anywhere on the host's file system can be mounted inside a container. This is useful for directly accessing the host's file system structure.

  • Creation and Management: They are directly dependent on the host's file system and are not explicitly managed by Docker.
  • Location: Any directory or file on the host's file system can be mounted into a container.
  • Characteristics: They are primarily useful in development environments for mounting source code into a container to reflect changes in real-time, or when a container needs to use a specific configuration file from the host.

Usage Examples

Understand how to manage data persistence by exploring various scenarios where Docker volume commands are practically applied.

Create and use a named volume

docker volume create my_db_data
docker run -d --name my-mysql --mount source=my_db_data,target=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword mysql:latest

Creates a volume named 'my_db_data' and mounts it to the data storage path of a MySQL container. The DB data will be preserved even if the container is deleted.

Bind mount a host directory

docker run -d --name my-nginx-dev -p 80:80 --mount type=bind,source=/path/to/my/app,target=/usr/share/nginx/html nginx:latest

Mounts the host's current directory (`/path/to/my/app`) to the web service directory of an Nginx container (`/usr/share/nginx/html`), so that changes made to files on the host are immediately reflected in the container.

Delete all unused volumes

docker volume prune

Frees up disk space by deleting all Docker volumes not attached to any container. A confirmation message will be displayed.

Force delete a specific volume (Warning)

docker volume rm -f old_volume

Forcefully deletes a volume named 'old_volume', whether it is in use or not. This should be used with extreme caution as it can lead to data loss.


Same category commands