AWS Logo
Menu
Docker Volume Commands

Docker Volume Commands

Discover Docker Volume commands: Create, inspect, and manage volumes to store persistent data and share it across containers easily.

Published Oct 2, 2024
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:
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:
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.
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:
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:
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:
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:

πŸ‘‰ 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:
In this command:
  • docker run: This is the command used to run a Docker container.
  • -v my_volume:/data: This specifies that the my_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:
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: 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:
Inspect the mysql image:
In the output, check out for volumes and here what I have
This means that docker will mount the volume at this location.
Create a container from MySQL image and list the volumes:
Here the volume with id starting 997 has been created at the location β€” /var/lib/mysql
Now let’s inspect the volume itself:
The mount point is where you will find the volume minted on the docker host:
Now I will go inside the container and create a few mysql databases and then remove the container:
Now, let’s remove the container:
Create a new container again:
And check the databases:
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:
Now to create a container with the volume id 997 already mounted on it, run the below command:
Verifying the same:
Β 

Comments