Docker Storage

Using Volumes for Persistent Storage

In this lesson, we will take a deeper look into using volumes with our Docker containers. Volumes are the preferred method for maintaining persistent data.

Volumes are easier to back up or migrate than bind mounts. You can manage volumes using Docker CLI commands or the Docker API. They work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers allow for:

  • Storing volumes on remote hosts or cloud providers

  • Encrypting the contents of volumes

  • Add other functionality

  • New volumes can have their content pre-populated by a container.

Step 1: Create a new volume for an Nginx container:

docker volume create html-volume

Step 2: Creating a volume using that volume mount:

docker container run -d \
 --name nginx-volume1 \
 --mount type=volume,source=html-volume,target=/usr/share/nginx/html/ \
 nginx

Step 3: Inspect the volume:

docker volume inspect html-volume

Step 4: List the contents of html-volume:

sudo ls /var/lib/docker/volumes/html-volume/_data

Step 5: Creating a volume using that volume flag:

Step 6: Edit index.html ( From outside the Container )

Step 7: Inspect nginx-volume2 to get the private IP:

Step 8: Login into nginx-volume1 and go to the html directory

Install Vim ( inside the container, if you dont have any editor )

Step 9: Using a readonly volume:

Step 10: Login into nginx-volume3 and go to the html directory:

Install Vim:

Last updated