# How to Create and Manage Docker Volumes with Practical Example

> URL: https://www.atlantic.net/vps-hosting/how-to-create-and-manage-docker-volumes-with-practical-example/ | Published: 2024-08-16 | Updated: 2024-08-19 | Author: Hitesh Jethva

Docker volumes are a crucial feature for managing data in containerized applications. They provide a way to persist data generated by Docker containers, ensuring that data remains intact even after the container is deleted.

In this guide, we will explore how to create and manage Docker volumes with practical examples.

## Types of Docker Volumes

1. **Named Volumes:** Named volumes are explicitly created and can be referenced by a specific name. They are useful when you want to share data between multiple containers or need to persist data beyond the lifecycle of a single container.
2. **Anonymous Volumes:** Anonymous volumes are created implicitly when a container is started and a volume is not specified. They are used when data persistence is needed, but the specific volume name is not important.
3. **Host Volumes:** Host volumes are created by mounting a directory from the host system into the container. This allows for direct access to host files and directories from within the container.

## Creating a Named Volume

To create a named volume, use the docker volume create command:

```bash
docker volume create my_named_volume
```

This command creates a volume named **my_named_volume.** You can verify its creation by listing all volumes:

```bash
docker volume ls
```

Output:

```bash
DRIVER    VOLUME NAME
local     my_named_volume
```

## Creating an Anonymous Volume

Anonymous volumes are automatically created when a container is started with a mount point, but no specific volume name is provided:

```bash
docker run -d --name my_container -v /data busybox
```

In this example, an anonymous volume is created and mounted to **/data** in the container.

## Mounting a Named Volume

You can mount a named volume into a container using the **-v** flag:

```bash
docker run -d --name my_new_container -v my_named_volume:/app busybox
```

Here, the named volume **my_named_volume** is mounted to **/app** in the container.

## Mounting a Host Volume

To mount a host directory as a volume, use the following syntax:

```bash
docker run -d --name my_host_container -v /mnt:/app busybox
```

This command mounts the host directory **/mnt** to **/app** in the container.

## Inspecting Docker Volumes

To inspect the details of a Docker volume, use the **docker volume inspect** command:

```bash
docker volume inspect my_named_volume
```

This command provides information about the volume, including its mount point and usage details.

```bash
[
    {
        "CreatedAt": "2024-07-25T09:41:19Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/my_named_volume/_data",
        "Name": "my_named_volume",
        "Options": null,
        "Scope": "local"
    }
]
```

## Removing Docker Volumes

Docker volumes can be removed using the docker volume rm command. Be cautious, as this action deletes the data stored in the volume.

To remove a named volume:

```bash
docker volume rm my_named_volume
```

To remove all volumes not currently in use by containers, use:

```bash
docker volume prune
```

Output.

```bash
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
```

## Conclusion

Docker volumes are a powerful tool for managing data in containerized environments. By understanding how to create and manage volumes, you can ensure data persistence, share data between containers, and manage application state effectively. Try to create and manage Docker volumes on [VPS hosting](https://www.atlantic.net/dedicated-server-hosting/) from Atlantic.Net!
