Rahul's Blog

Docker Volume

August 01, 2020

Docker Volumes

Volumes are the preferred mechanism for persisting data generated by and used by Docker Containers.

Data is stored inside the container on a writable container layer by default. It gets deleted on deleting the container. But in enterprise applications we do not want to delete the data.

A container’s writable layer doesn’t persist when that container is no longer running.

A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.

Use of Volumes

  • decoupling container from storage
  • share volume(storage/data) among different containers
  • attach volume to container
  • on deleting container volume does not delete

To get info about docker volume

# get information
$ docker volume

Create docker volume

# create volume
$ docker volume create <volume-name>

# Example
$ docker volume create vol1

List docker volumes

# list the volumes
$ docker volume ls

Inspect docker volume

# inspect docker volume
$ docker volume inspect <volume-name>

Remove docker volume

# remove the volume
$ docker volume rm <volume-name>
# remove all unused volumes
$ docker volume prune

# and press Y in prompt

Using volume in docker containers

# assigning volume to the container
$ docker container run -d --name <container-name> -v <volume-name>:<path-to-store-data> <image>

# Example 
$ docker container run -d --name MyNginx1 -v vol1:/var/nginx_home -p 8080:8080 nginx

bind mount

It is used to bind host machine directory with container directory

# mount physical directory to the container
$ docker container run -d --name <container-name> -v <directory-path>:<path-to-store-data> <image>

# Example 
$ docker container run -d --name MyNginx1 -v /home/rahul:/var/nginx_home -p 8080:8080 nginx

The file or directory does not need to exist on the docker host already. It is created on demand if it does not exist. We can use physical location instead of volume too to store the data

More about docker volumes

Docker has 2 options for containers to store files in the host machine, so that the file are persisted even after the container stops

  1. Volumes
  2. Bind Mounts

Volumes are stored in a part of the filesystem which is managed by Docker. Non-docker processes should not modify this part of the filesystem.

Bind mounts may be stored anywhere on the host system. Non-docker processes on the Docker host or a Docker Container can modify them any time.

In bind mounts, the file or directory is referenced by its full path on the host machine.

Volumes are the best way to persist data in docker. Volumes are managed by docker and are isolated from the core functionality of the host machine.

A given volume can be mounted into multiple containers simultaneously.

When no running container is using a volume, the volume is still available to Docker and is not removed automatically. You can remove unused volumes using docker volume prune.

When you mount a volume, it may be named or anonymous.

Anonymous volumes are not given an explicit name when they are first mounted into a container.

Volumes also support the use of volume drivers, which allow you to store your data on remote hosts or cloud providers, among other possibilities.


Written by Rahul Jain         
I write code like I make sandwich