🎉 Article Published: 99

if you are not a medium member then Click here to read free

The fundamental concepts of Docker are:

  1. Images — The blueprint for containers, containing application code and dependencies.
  2. Containers — Running instances of images, isolated and lightweight.
  3. Dockerfile — A script that defines how to build a Docker image.
  4. Volumes — Persistent storage for data inside containers.
  5. Networking — Communication between containers and the external world.
  6. Docker Compose — A tool for managing multi-container applications.
  7. Registry — A storage location for Docker images (e.g., Docker Hub).

Today we are going to learn one of the fundamental concepts called volumes

What is a Docker Volume?

A Docker volume is like a shared folder or external hard drive that your Docker containers can use to store and access data. It exists outside the container, so even if the container is deleted, the data in the volume remains safe.

Why Use Docker Volumes?

  1. Persistent Storage: Containers are temporary. If you delete a container, all the data inside it is lost. Volumes allow you to save data permanently, even after the container is gone.
  2. Share Data: Multiple containers can use the same volume to share data.
  3. Backup and Migration: Volumes make it easy to back up or move data to another system.

Example to Understand Better

Imagine you have a container running a database (like MySQL). The database stores all its data inside the container. If you delete the container, the data is gone forever. To avoid this, you can use a Docker volume to store the database data outside the container. Now, even if you delete the container, the data stays safe in the volume

Now I explain the different Key Features of Docker Volumes

1. What is Persistent Storage in Docker?

When you run a container, any data inside it will be lost once the container is removed. To store data permanently, you need Persistent Storage, which means data will stay even if the container stops or is deleted.

Real-World Example: Running MySQL in Docker

Imagine you are running a MySQL database in Docker. If you store data inside the container without persistent storage, when the container restarts, all the data will be lost. To solve this, we use volumes to store MySQL data outside the container, so it remains even if the container is deleted.

How to Create Persistent Storage in Docker?

We use Docker Volumes (preferred) or Bind Mounts to store data persistently.

Step 1: Create a Volume

docker volume create mysql_data

This creates a named volume called mysql_data

Step 2: Run a MySQL Container with Persistent Storage

docker run -d \
--name my-mysql \
-e MYSQL_ROOT_PASSWORD=root \
-v mysql_data:/var/lib/mysql \
mysql:latest

Explanation

  • -d → Runs the container in the background.
  • --name my-mysql → Names the container my-mysql.
  • -e MYSQL_ROOT_PASSWORD=root → Sets MySQL root password.
  • -v mysql_data:/var/lib/mysqlMounts the volume mysql_data to /var/lib/mysql, where MySQL stores its data.
  • mysql:latest → Uses the latest MySQL image.

Step 3: Verify That Persistent Storage Works

Stop and Remove the Container

docker stop my-mysql
docker rm my-mysql

Even though the container is removed, data is not lost because it is stored in the volume.

Step 4: Start a New MySQL Container and Use the Same Volume

docker run -d \
--name new-mysql \
-e MYSQL_ROOT_PASSWORD=root \
-v mysql_data:/var/lib/mysql \
mysql:latest

Now, the new container still has all the previous data from mysql_data

2. Key Features of Docker Volumes

2. what is Shared Storage Between Containers

Shared Storage Between Containers means that multiple Docker containers can access and modify the same data using a common storage location. This is achieved using Docker Volumes or Bind Mounts, ensuring data persistence, consistency, and easy sharing across containers.

Use Case: Shared Storage in a Microservices Architecture (Logging Service Example)

Scenario:

You have two microservices running in separate containers:

  1. Application Service (app-service) – This service generates logs and writes them to a shared directory.
  2. Logging Service (log-service) – This service reads the logs from the shared directory and processes them (e.g., uploading logs to a centralized log management system).

Goal:

Both containers should have access to the same log files using a shared Docker volume.

Step-by-Step Implementation

Step 1: Create a Named Volume

We create a shared volume to store logs:

docker volume create logs_volume

Step 2: Run the Application Service with the Shared Volume

This container writes logs to /var/log/app inside the volume:

docker run -d --name app-service -v logs_volume:/var/log/app ubuntu

Inside app-service, logs will be written to /var/log/app.

To simulate log writing, run

docker exec -it app-service bash -c "echo 'Error: Something went wrong' > /var/log/app/error.log"

Step 3: Run the Logging Service with the Same Shared Volume

Now, we start another container (log-service) that reads logs from the shared volume

docker run -d --name log-service -v logs_volume:/var/log/app ubuntu

ince both containers use logs_volume, they share /var/log/app.

To verify, run:

docker exec -it log-service cat /var/log/app/error.log

Output

Error: Something went wrong

This confirms that the log-service can read the logs generated by app-service

3 . Key Features of Docker Volumes

3. what is Backup & Restore Volumes in Docker

Real-Time Use Case: Backup & Restore Volumes in a Database Container

Scenario:

Imagine you're managing a PostgreSQL database running inside a Docker container. The database stores critical customer transaction data, and you need to:

  1. Backup the database volume daily to prevent data loss.
  2. Restore the database in case of server failure or migration to a new machine

Step-by-Step Implementation

Step 1: Start a PostgreSQL Container with a Volume

First, create a Docker volume to store database data:

docker volume create pg_data

Now, start a PostgreSQL container using this volume

docker run -d --name my_postgres -e POSTGRES_PASSWORD=mysecret -v pg_data:/var/lib/postgresql/data postgres

Explanation:

  • -v pg_data:/var/lib/postgresql/data: Mounts pg_data volume to the PostgreSQL data directory.
  • -e POSTGRES_PASSWORD=mysecret: Sets the database password.
  • postgres: Uses the official PostgreSQL image.

The database will store all data inside the pg_data volume

Step 2: Backup the PostgreSQL Volume

To create a backup, run

docker run --rm -v pg_data:/data -v $(pwd):/backup ubuntu tar cvf /backup/postgres_backup.tar /data

Explanation:

  • --rm: Automatically removes the container after the backup.
  • -v pg_data:/data: Mounts the pg_data volume to /data in the container.
  • -v $(pwd):/backup: Mounts the current directory ($(pwd)) as /backup to store the backup.
  • tar cvf /backup/postgres_backup.tar /data: Creates a tar archive of the /data directory.

Now, postgres_backup.tar is saved in your current directory as a backup of your database volume.

Step 3: Restore the PostgreSQL Volume (After Server Crash or Migration)

If the database server crashes or you need to move to a new machine, you can restore the backup.

1️ .Create a New Volume for Restoration

docker volume create pg_data_restore

2️. Restore Data from Backup

docker run --rm -v pg_data_restore:/data -v $(pwd):/backup ubuntu tar xvf /backup/postgres_backup.tar -C /data

Explanation:

  • -v pg_data_restore:/data: Mounts the new volume.
  • tar xvf /backup/postgres_backup.tar -C /data: Extracts the backup data into the volume.

3. Start a New PostgreSQL Container Using the Restored Volume

docker run -d --name restored_postgres -e POSTGRES_PASSWORD=mysecret -v pg_data_restore:/var/lib/postgresql/data postgres

Now, your database is restored and ready to use

4.Key Features of Docker Volumes

4.Read-Only Mode in Docker Volumes (Prevent Accidental Changes)

What is Read-Only Mode?

Docker allows you to mount a volume in read-only mode, meaning the container can only read the data but cannot modify or delete it. This prevents accidental changes to critical files, making it useful for configuration files, log files, and backups

Real-World Use Case: Preventing Accidental Changes to Configuration Files

Scenario:

You are running an NGINX web server inside a Docker container. The server uses a configuration file (nginx.conf) that should never be modified by the container to avoid breaking the server

Step-by-Step Implementation

Step 1: Create a Configuration File

Create an nginx.conf file on your local machine

echo "server { listen 80; server_name example.com; }" > nginx.conf

Step 2: Run the NGINX Container with Read-Only Volume

Mount the nginx.conf file as read-only inside the container

docker run -d --name my_nginx -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginx

Explanation:

  • -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro → Mounts nginx.conf inside the container in read-only mode (:ro).
  • The container can read the config but cannot edit or delete it

Step 3: Verify Read-Only Behavior

Try modifying the file inside the container

docker exec -it my_nginx bash -c "echo 'New Config' >> /etc/nginx/nginx.conf"

Error:

bash: /etc/nginx/nginx.conf: Read-only file system

This confirms that the file cannot be modified, preventing accidental changes

Thank You for Reading!

If you found this content helpful, feel free to show your support with 👏 claps! 😊 Your encouragement keeps me motivated to write more articles

You can go through my other articles

"Follow me on Medium to never miss an update!"

If you found this content helpful, feel free to show your support with 👏 claps! 😊 Your encouragement keeps me motivated to write more articles