CodeGym /Courses /Docker SELF /Creating and managing volumes

Creating and managing volumes

Docker SELF
Level 19 , Lesson 1
Available

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:

  1. docker volume create
  2. docker volume ls
  3. docker volume inspect
  4. docker volume rm
  5. 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.

Terminal

docker volume create my_volume

Creating a Volume with Settings

You can specify a driver and options to create a volume with specific characteristics.

Terminal

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.

Terminal

docker volume ls

The output will include information about all existing volumes:

Terminal

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.

Terminal

docker volume inspect my_volume

The output of the command will include information in JSON format:

JSON

[
    {
        "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.

Terminal

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.

Terminal

docker volume prune

2.6 Practical Examples of Using Commands

Example 1: Creating and Using a Volume in a Container

Creating a volume:

Terminal

docker volume create app_data

Running a container with a volume:

Terminal

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:

Terminal

docker volume create logs_data

Running a container with a volume:

Terminal

docker run -d --name log_app -v logs_data:/var/log/app busybox

Viewing volume information:

Terminal

docker volume inspect logs_data

Example 3: Deleting and Cleaning Up Volumes

Creating and using a volume:

Terminal

docker volume create temp_data

docker run -d --name temp_app -v temp_data:/tmp busybox

Stopping and removing a container:

Terminal

docker stop temp_app

docker rm temp_app

Deleting a volume:

Terminal

docker volume rm temp_data

Cleaning up all unused volumes:

Terminal

docker volume prune
3
Task
Docker SELF, level 19, lesson 1
Locked
Creating and using a volume
Creating and using a volume
3
Task
Docker SELF, level 19, lesson 1
Locked
Viewing volume information
Viewing volume information
3
Task
Docker SELF, level 19, lesson 1
Locked
Removing a Volume
Removing a Volume
3
Task
Docker SELF, level 19, lesson 1
Locked
Cleaning up unused volumes
Cleaning up unused volumes
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION