Saiyam Pathak avatar
By Saiyam Pathak
Field CTO


Learn how to create Docker volumes with Civo Academy. Understand the importance of data persistence in Docker and explore the creation of named and anonymous volumes.

In this lesson, we delve into the fascinating world of Docker volumes. If you've ever wondered what happens to your data when a Docker container is removed or restarted, you're in for a treat.

Understanding Docker Volumes

In the Docker ecosystem, data within a container is not persistent. This means that when a container is restarted, stopped, or removed, the data within the container also gets lost. When a new container is spun up, it's a fresh container with a new state. However, in many scenarios, we need our data to be persistent. This is where Docker volumes come into play. Docker volumes are used to persist data, especially for databases or stateful applications.

Creating a Docker Volume

When creating a Docker volume, we first use the Docker run command, followed by '-it -v`, as such `docker, run -it -v`. The `-v` command is used to specify the Docker volume. Next, we provide a host path, such as `/home`, and specify the path within the container that we want to mount to the host file system, such as `/temp` of the busybox container. The complete command would be `docker run -it -v /home:/temp/ busybox`.

Once the command is executed, Docker pulls the latest image, and we find ourselves inside the busybox shell. We can then navigate to the temp directory and create a test file using the command `tmp # touch test`. After creating the file, we can exit the container using the `exit` command. Docker will then persist the data inside the host path that we provided, and the data will also be visible in the container. This process is known as host mounting inside the Docker container.

Creating Named Volumes

Docker also allows us to create named volumes, which are managed by Docker itself. We can use the command `docker run -it -v demo:/usr/share busybox` to create a named volume. This command creates a named volume called "demo" and mounts it to the `/usr/share` directory of the busybox container.

After creating a file in the mounted directory and exiting the container, Docker automatically creates a "demo" volume. We can view the created volumes using the command `docker volume ls`. The created volumes are stored in the directory `cd var/lib/docker/volumes`.

Creating Anonymous Volumes

If we don't specify a name when creating a volume, Docker automatically generates an anonymous volume with a random hash. Even though the volume name is randomly generated, the data is still persisted. Anonymous volumes can be created using the command `docker run -it -v /home busybox`.


In this lesson, we've learned how to create Docker volumes and persist data on the host file system or a volume managed by Docker. Named volumes are the most widely used due to their ease of management.

Civo course complete badge

You've successfully completed our course on Before Kubernetes

We hope you enjoyed learning and encourage you to check out our other courses to further expand your knowledge on Kubernetes