The docker volume
command is used to manage Docker volumes. It provides various subcommands to create, inspect, list, remove, and prune volumes. Here's an overview of the docker volume
command and its subcommands:
👉Create a Volume:
Command: docker volume create[VOLUME_NAME]:
Creates a new named volume with the specified name or lets Docker generate a unique name if not provided.
Example:
docker volume create my_volume
This command creates a Docker volume named my_volume
on your Docker host. Once created, this volume can be used to persist data generated by containers or share data between containers.
👉 Inspect a Volume:
Command: docker volume inspect [VOLUME_NAME]:
Displays detailed information about one or more volumes, including metadata and options.
Example:
docker volume inspect my_volume
This command inspects the volume named my_volume
and provides detailed information about its configuration and properties. It returns a JSON-formatted output containing various details such as the volume's name, driver, mount point, labels, and other metadata.
For instance, if you have previously created the my_volume
volume using the docker volume create
command, running the above docker volume inspect
command will display information about this volume, including its name, driver, and any labels associated with it.
[
{
"CreatedAt": "2022-02-17T15:35:40+01:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my_volume/_data",
"Name": "my_volume",
"Options": {},
"Scope": "local"
}
]
This output provides details about the my_volume
volume, such as its creation time, driver type (in this case, it's a local volume), mount point, and other relevant information.
👉 List Volumes:
Command: docker volume ls:
Lists all volumes on the Docker host, showing volume names and their respective driver and options.
Example:
docker volume ls
This command lists all Docker volumes present on your Docker host. When you run this command, you’ll see a list of volumes with their respective names and drivers.
For example, if you have several volumes created on your system, the output might look something like this:
DRIVER VOLUME NAME
local my_volume
local another_volume
In this example, there are two volumes listed: my_volume
and another_volume
. Both volumes are using the local volume driver (local
), which means they are stored locally on the Docker host.
👉 Remove a Volume:
Command: docker volume rm [VOLUME_NAME]:
Removes one or more volumes. Docker prevents removal of volumes that are currently in use by containers.
Example:
docker volume rm my_volume
This command removes the Docker volume named my_volume
from your Docker host. Once executed, the volume and any associated data will be permanently deleted, so be cautious when using this command.
If the volume is currently being used by a container, Docker will prevent you from removing it and display an error message indicating that the volume is still in use. You must stop and remove any containers that are using the volume before you can successfully remove it.
👉 Prune Unused Volumes:
Command: docker volume prune [OPTIONS]:
Removes all volumes not used by at least one container. Use with caution, as it permanently deletes data.
Example:
docker volume prune
👉 Mount a Volume:
Command: docker run -v [VOLUME]:[CONTAINER_PATH] [IMAGE] [COMMAND]:
Mounts a volume into a container at the specified path. Data written to the path inside the container is persisted in the volume.
Example:
docker run -v my_volume:/data my_image
In this command:
docker run
: This is the command used to run a Docker container.-v my_volume:/data
: This specifies that themy_volume
volume should be mounted into the container at the/data
directory.my_image
: This is the name of the Docker image you want to run as a container.
When you run this command, Docker will create a new container based on the specified image (my_image
). The my_volume
volume will be mounted into the container at the /data
directory. Any data written to the /data
directory within the container will be stored in the my_volume
volume on the Docker host.
This allows you to persist data generated by the container in the my_volume
volume, ensuring that it remains available even if the container is stopped or removed.
👉 Bind Mount a Host Directory:
Command: docker run -v [HOST_PATH]:[CONTAINER_PATH] [IMAGE] [COMMAND]:
Binds a directory on the Docker host to a path inside the container. Changes to files in the directory are reflected inside the container and vice versa.
Example:
docker run -v /host/path:/container/path my_image
In this command:
docker run
: This is the command used to run a Docker container.-v /host/path:/container/path
: This specifies that the/host/path
directory on the Docker host should be mounted into the container at the/container/path
directory.my_image
: This is the name of the Docker image you want to run as a container.
When you run this command, Docker will create a new container based on the specified image (my_image
). The /host/path
directory on the Docker host will be mounted into the container at the /container/path
directory. Any files or directories present in /host/path
on the host machine will be accessible from /container/path
inside the container.
This allows you to share files or directories between the host and the container, making it useful for development or providing configuration files to the container.
Note: Before running this command, make sure that the /host/path
directory exists on your Docker host. If it doesn't exist, Docker will create it as a directory inside the container.
👉Creating Containers with Data Volumes
The command you provided creates a Docker container based on the mysql
image and attaches an existing volume (997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395
) to the container's /var/lib/mysql
directory.
Here’s the breakdown of the command:
docker container run -itd -v 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395:/var/lib/mysql mysql
docker container run
: This command is used to create and run a new Docker container.-itd
: These flags are used to run the container in detached mode (-d
), allocate a pseudo-TTY (-t
), and keep STDIN open even if not attached (-i
).-v 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395:/var/lib/mysql
: This flag mounts the specified volume (997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395
) into the container's/var/lib/mysql
directory.mysql
: This is the name of the Docker image used to create the container.
The volume 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395
is likely a named volume or a bind-mounted host directory that contains MySQL data. By attaching this volume to the container's /var/lib/mysql
directory, you ensure that the MySQL data persists even if the container is stopped or removed.
👉 A Step by Step Guide to Docker Volume
First list the images available
localhost:~$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c60d96bd2b47 11 days ago 514MB
ubuntu 14.04 13b66b487534 4 months ago 197MB
localhost:~$
Inspect the mysql image
localhost:~$ docker image inspect mysql
In the output, check out for volumes and here what I have
localhost:~$ docker image inspect mysql
[
{
# output removed
"Volumes": {
"/var/lib/mysql": {}
},
# output removed
}
]
This means that docker will mount the volume at this location.
Create a container from MySQL image and list the volumes
localhost:~$ docker volume ls
localhost:~$ docker container run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
2a7aadab7cf6bda9fdc35810227517eaa5e97d2cdedfbe7e1583c028a37a3556
localhost:~$ docker volume ls
DRIVER VOLUME NAME
local 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295
localhost:~$
Here the volume with id starting 997 has been created at the location — /var/lib/mysql
Now let’s inspect the volume itself
localhost:~$ docker volume inspect 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295
[
{
"CreatedAt": "2021-08-02T16:08:35Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395/_data",
"Name": "997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295",
"Options": null,
"Scope": "local"
}
]
localhost:~$
The mount point is where you will find the volume minted on the docker host
localhost:~$ sudo su
[sudo] password for ubuntu:
localhost:/home/ubuntu# cd /var/lib/docker/volumes/997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295/_data/
localhost:/var/lib/docker/volumes/997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b395/_data# ls
auto.cnf client-key.pem ibtmp1 server-cert.pem
binlog.000001 '#ib_16384_0.dblwr' '#innodb_temp' server-key.pem
binlog.000002 '#ib_16384_1.dblwr' mysql sys
binlog.index ib_buffer_pool mysql.ibd undo_001
ca-key.pem ibdata1 performance_schema undo_002
ca.pem ib_logfile0 private_key.pem
client-cert.pem ib_logfile1 public_key.pem
localhost:/var/lib/docker/volumes/997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295/_data# exit
Now I will go inside the container and create a few mysql databases and then remove the container
localhost:~$ docker container exec -it 2a7 bash
root@2a7aadab7cf6:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database learingocean;
Query OK, 1 row affected (0.01 sec)
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
mysql> create database example;
Query OK, 1 row affected (0.01 sec)
mysql> exit
Bye
root@2a7aadab7cf6:/# exit
exit
localhost:~$
Now, let’s remove the container
localhost:~$ docker container rm -f 2a
2a
Create a new container again
localhost:~$ docker container run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql
1fec03b43712be52967713d2f9f9f8d367a6926faccbb206fa07c4bcbcb43deb
localhost:~$ docker container exec -it 1f bash
root@1fec03b43712:/#
And check the databases
root@1fec03b43712:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
None of the databases we created and all the data is lost.
We can also create a new container with pre-populated data or say the existing volume. Let’s see how we create a new container with already existing volume.
We have these volumes as of now
localhost:~$ docker volume ls
DRIVER VOLUME NAME
local 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295
local f64908bbe87a99d487b35110c6335e63e5e91fc174088e610c87571d32c150f9
localhost:~$
Now to create a container with the volume id 997 already mounted on it, run the below command
localhost:~$ docker container run -itd -v 997a7e220305eb30a9307c686648f9417f06509e0c9ce28cc4fc2028c513b295:/var/lib/mysql mysql
a9b9f511d66571b993fa2084de0a2f2dfafab416017c1d2081d8f9ccbc025c46
Verifying the same
localhost:~$ docker container exec -it a9 bash
root@a9b9f511d665:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| example |
| information_schema |
| learingocean |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
7 rows in set (0.01 sec)
mysql>