2.1 Basic docker volume
Commands
Volumes in Docker provide persistent storage for container data and make it easy to manage and isolate data. The docker volume
command offers a set of tools for creating, managing, and deleting volumes. In this lecture, we’ll go over how to use the docker volume
command to efficiently manage volumes in Docker.
Key docker volume commands:
docker volume create
docker volume ls
docker volume inspect
docker volume rm
docker volume prune
2.2 Creating a Volume: docker volume create
The docker volume create
command is used to create a new volume. By default, Docker creates the volume in the standard location on the host, but you can also specify different options to configure the volume.
Simple Example of Creating a Volume
This example will create a volume named my_volume
.
docker volume create my_volume
Creating a Volume with Settings
You can specify a driver and options to create a volume with specific characteristics.
docker volume create --driver local --opt o=uid=1000 --opt o=gid=1000 --opt type=tmpfs --opt
device=tmpfs my_custom_volume
We’ll cover all these options below, so don’t worry too much about them for now.
2.3 Volume List: docker volume ls
The docker volume ls
command shows a list of all volumes created on the host.
docker volume ls
The output will include information about all existing volumes:
DRIVER VOLUME NAME
local my_volume
local my_custom_volume
2.4 Volume Information: docker volume inspect
The docker volume inspect
command gives detailed info about a specific volume, including its location, parameters, and usage.
docker volume inspect my_volume
The output of the command will include information in JSON format:
[
{
"CreatedAt": "2023-07-26T12:34:56Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my_volume/_data",
"Name": "my_volume",
"Options": {},
"Scope": "local"
}
]
2.5 Deleting a volume: docker volume rm
The docker volume rm
command is used to delete a volume. It's important to make sure the volume isn't being used by any containers before deleting it.
Deleting a volume
If the volume is in use, Docker will throw an error saying that the volume cannot be removed.
docker volume rm my_volume
Cleaning up unused volumes: docker volume prune
The docker volume prune
command removes all unused volumes, which helps free up disk space.
Cleaning up unused volumes
After running the command, Docker will ask for confirmation before deleting all unused volumes.
docker volume prune
2.6 Practical Examples of Using Commands
Example 1: Creating and Using a Volume in a Container
Creating a volume:
docker volume create app_data
Running a container with a volume:
docker run -d --name my_app -v app_data:/usr/share/nginx/html nginx
In this example, volume app_data
is mounted to the /usr/share/nginx/html
directory of the nginx
container.
Example 2: Getting Volume Information
Creating a volume:
docker volume create logs_data
Running a container with a volume:
docker run -d --name log_app -v logs_data:/var/log/app busybox
Viewing volume information:
docker volume inspect logs_data
Example 3: Deleting and Cleaning Up Volumes
Creating and using a volume:
docker volume create temp_data
docker run -d --name temp_app -v temp_data:/tmp busybox
Stopping and removing a container:
docker stop temp_app
docker rm temp_app
Deleting a volume:
docker volume rm temp_data
Cleaning up all unused volumes:
docker volume prune
GO TO FULL VERSION