DEV Community

Arman Chand
Arman Chand

Posted on

Volumes in Docker

Problem Statement

It is a very common requirement to persist the data in a Docker container beyond the lifetime of the container. However, the file system
of a Docker container is deleted/removed when the container dies.

Solution

There are 2 different ways how docker solves this problem.

  1. Volumes
  2. Bind Directory on a host as a Mount

Volumes

Volumes aims to solve the same problem by providing a way to store data on the host file system, separate from the container's file system,
so that the data can persist even if the container is deleted and recreated.

image

Volumes can be created and managed using the docker volume command. You can create a new volume using the following command:

docker volume create <volume_name>
Enter fullscreen mode Exit fullscreen mode

Once a volume is created, you can mount it to a container using the -v or --mount option when running a docker run command.

For example:

docker run -it -v <volume_name>:/data <image_name> /bin/bash
Enter fullscreen mode Exit fullscreen mode

This command will mount the volume to the /data directory in the container. Any data written to the /data directory
inside the container will be persisted in the volume on the host file system.

Bind Directory on a host as a Mount

Bind mounts also aims to solve the same problem but in a complete different way.

Using this way, user can mount a directory from the host file system into a container. Bind mounts have the same behavior as volumes, but
are specified using a host path instead of a volume name.

For example,

docker run -it -v <host_path>:<container_path> <image_name> /bin/bash
Enter fullscreen mode Exit fullscreen mode

Key Differences between Volumes and Bind Directory on a host as a Mount

Volumes are managed, created, mounted and deleted using the Docker API. However, Volumes are more flexible than bind mounts, as
they can be managed and backed up separately from the host file system, and can be moved between containers and hosts.

In a nutshell, Bind Directory on a host as a Mount are appropriate for simple use cases where you need to mount a directory from the host file system into
a container, while volumes are better suited for more complex use cases where you need more control over the data being persisted
in the container.

Top comments (0)