Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Docker Persistent Volume Demo

This tutorial demonstrates how to use Docker volumes to persist data across container restarts and rebuilds using Nginx as a web server.

Project Structure

Lab2/
├── Dockerfile
├── docker-compose.yml
├── html/
│   └── index.html
└── README.md

Prerequisites

  • Docker installed on your machine
  • Basic understanding of Docker concepts

Option 1: Using Docker CLI

Step 1: Build the Docker image

docker build -t nginx-volume-demo .

Step 2: Create a Docker volume

docker volume create web-content

Step 3: Run the container with a volume mount

docker run -d --name nginx-volume-container \
  -p 8080:80 \
  -v $(pwd)/html:/usr/share/nginx/html \
  nginx-volume-demo

Alternative: Using a named volume instead of a bind mount

docker run -d --name nginx-volume-container \
  -p 8080:80 \
  -v web-content:/usr/share/nginx/html \
  nginx-volume-demo

Step 4: Access the website

Open your browser and navigate to http://localhost:8080

Step 5: Test persistence

  1. Modify the html/index.html file
  2. Refresh your browser to see changes immediately
  3. Stop and remove the container:
    docker stop nginx-volume-container
    docker rm nginx-volume-container
  4. Start a new container with the same volume:
    docker run -d --name nginx-volume-container-new \
      -p 8080:80 \
      -v $(pwd)/html:/usr/share/nginx/html \
      nginx-volume-demo
  5. Verify your changes are still there at http://localhost:8080

Option 2: Using Docker Compose

Step 1: Start services with Docker Compose

docker-compose up -d

Step 2: Access the website

Open your browser and navigate to http://localhost:8080

Step 3: Test persistence

  1. Modify the html/index.html file
  2. Refresh your browser to see changes immediately
  3. Stop and remove containers:
    docker-compose down
  4. Start services again:
    docker-compose up -d
  5. Verify your changes are still there at http://localhost:8080

Understanding Docker Volumes

What are Docker Volumes?

Docker volumes are the preferred way to persist data generated by and used by Docker containers. Unlike bind mounts, volumes are completely managed by Docker.

┌──────────────────────────────────────────────────────────┐
│                                                          │
│                Docker Volume Architecture                │
│                                                          │
│  ┌─────────────┐        ┌─────────────┐                  │
│  │ Container A │        │ Container B │                  │
│  │             │        │             │                  │
│  │ ┌─────────┐ │        │ ┌─────────┐ │                  │
│  │ │Mount    │ │        │ │Mount    │ │                  │
│  │ │Point    │ │        │ │Point    │ │                  │
│  │ └────┬────┘ │        │ └────┬────┘ │                  │
│  └──────┼──────┘        └──────┼──────┘                  │
│         │                      │                         │
│         │                      │                         │
│         ▼                      ▼                         │
│  ┌──────────────────────────────────────┐                │
│  │                                      │                │
│  │           Docker Volume              │                │
│  │                                      │                │
│  └──────────────────────────────────────┘                │
│                     │                                    │
│                     │                                    │
│                     ▼                                    │
│  ┌──────────────────────────────────────┐                │
│  │                                      │                │
│  │     Docker's Storage Location        │                │
│  │  (e.g., /var/lib/docker/volumes)     │                │
│  │                                      │                │
│  └──────────────────────────────────────┘                │
│                                                          │
└──────────────────────────────────────────────────────────┘

Types of Volumes Demonstrated

  1. Bind Mounts: We're mapping a directory from the host (./html) to a directory in the container (/usr/share/nginx/html).
┌──────────────────────────────────────────────────────────┐
│                   Bind Mount Workflow                    │
│                                                          │
│  ┌─────────────────┐           ┌─────────────────┐       │
│  │   Host System   │           │    Container    │       │
│  │                 │           │                 │       │
│  │ ┌─────────────┐ │           │ ┌─────────────┐ │       │
│  │ │             │ │  mapped   │ │             │ │       │
│  │ │  ./html     │◄├───────────┤►│/usr/share/  │ │       │
│  │ │  directory  │ │  directly │ │nginx/html   │ │       │
│  │ │             │ │           │ │             │ │       │
│  │ └─────────────┘ │           │ └─────────────┘ │       │
│  │                 │           │                 │       │
│  └─────────────────┘           └─────────────────┘       │
│                                                          │
└──────────────────────────────────────────────────────────┘
  1. Named Volumes: Shown in the Docker CLI alternative approach.
┌──────────────────────────────────────────────────────────┐
│                  Named Volume Workflow                   │
│                                                          │
│  ┌─────────────────┐           ┌─────────────────┐       │
│  │  Docker Volume  │           │    Container    │       │
│  │                 │           │                 │       │
│  │ ┌─────────────┐ │           │ ┌─────────────┐ │       │
│  │ │             │ │  mounted  │ │             │ │       │
│  │ │web-content  │◄├───────────┤►│/usr/share/  │ │       │
│  │ │  volume     │ │    to     │ │nginx/html   │ │       │
│  │ │             │ │           │ │             │ │       │
│  │ └─────────────┘ │           │ └─────────────┘ │       │
│  │                 │           │                 │       │
│  └─────────────────┘           └─────────────────┘       │
│           │                                              │
│           │                                              │
│           ▼                                              │
│  ┌─────────────────┐                                     │
│  │  Docker Managed │                                     │
│  │  Storage Area   │                                     │
│  └─────────────────┘                                     │
│                                                          │
└──────────────────────────────────────────────────────────┘

Volume Persistence Demonstration

┌──────────────────────────────────────────────────────────┐
│                Volume Data Persistence                   │
│                                                          │
│  ┌────────────┐   Stop &    ┌────────────────┐           │
│  │ Container 1│   Remove    │   Container 2  │           │
│  │            │             │                │           │
│  │            │ ────────►   │                │           │
│  │            │   then      │                │           │
│  │            │   create    │                │           │
│  └──────┬─────┘             └────────┬───────┘           │
│         │                            │                   │
│         │                            │                   │
│         ▼                            ▼                   │
│  ┌──────────────────────────────────────────────┐        │
│  │                                              │        │
│  │              Same Volume                     │        │
│  │         (Data is preserved)                  │        │
│  │                                              │        │
│  └──────────────────────────────────────────────┘        │
│                                                          │
└──────────────────────────────────────────────────────────┘

Key Benefits

  • Data Persistence: Data persists independently of container lifecycle
  • Host-Container Sharing: Easy sharing of data between host and container
  • Performance: Improved performance (especially for databases or file-intensive applications)
  • Multi-container Access: Can be shared across multiple containers
┌──────────────────────────────────────────────────────────┐
│                  Volume Benefits                         │
│                                                          │
│  ┌─────────────┐     ┌─────────────┐     ┌─────────────┐ │
│  │ Container A │     │ Container B │     │ Container C │ │
│  │ (Web Server)│     │ (Backup     │     │ (Dev        │ │
│  │             │     │  Process)   │     │  Process)   │ │
│  └──────┬──────┘     └──────┬──────┘     └──────┬──────┘ │
│         │                   │                   │        │
│         │                   │                   │        │
│         ▼                   ▼                   ▼        │
│  ┌─────────────────────────────────────────────────┐     │
│  │                                                 │     │
│  │                Shared Volume                    │     │
│  │                                                 │     │
│  └─────────────────────────────────────────────────┘     │
│                                                          │
└──────────────────────────────────────────────────────────┘

Bind Mount vs. Named Volume Comparison

┌───────────────────────────────────────────────────────────┐
│             Volume Type Comparison                        │
│                                                           │
│  ┌───────────────────┐       ┌───────────────────┐        │
│  │    Bind Mount     │       │   Named Volume    │        │
│  ├───────────────────┤       ├───────────────────┤        │
│  │ • Host path based │       │ • Docker managed  │        │
│  │ • Good for dev    │       │ • Better for prod │        │
│  │ • Direct access   │       │ • Portable        │        │
│  │   from host       │       │ • Easier to back  │        │
│  │ • Follows host    │       │   up and restore  │        │
│  │   filesystem      │       │ • Content isolated│        │
│  │   permissions     │       │   from host       │        │
│  └───────────────────┘       └───────────────────┘        │
│                                                           │
└───────────────────────────────────────────────────────────┘

Common Use Cases

  • Databases: MySQL, PostgreSQL, MongoDB
  • Web Server Content: As demonstrated in this example
  • Application Logs: Persistent logging across container restarts
  • Configuration Files: Sharing configuration across container instances

Volume Lifecycle Management

┌────────────────────────────────────────────────────────────┐
│                   Volume Lifecycle                         │
│                                                            │
│                                                            │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐     │
│  │ 1. Create   │    │ 2. Use with │    │ 3. Persist  │     │
│  │    Volume   │───►│  Container  │───►│    Data     │     │
│  │             │    │             │    │             │     │
│  └─────────────┘    └─────────────┘    └──────┬──────┘     │
│         ▲                                     │            │
│         │                                     │            │
│         │                                     ▼            │
│  ┌─────────────┐                        ┌────────────────┐ │
│  │ 5. Remove   │◄───────────────────────┤ 4. Stop/Remove │ │
│  │    Volume   │                        │   Container    │ │
│  │  (Optional) │                        │                │ │
│  └─────────────┘                        └────────────────┘ │
│                                                            │
└────────────────────────────────────────────────────────────┘

Cleaning Up

# For Docker CLI approach
docker stop nginx-volume-container
docker rm nginx-volume-container
docker volume rm web-content

# For Docker Compose approach
docker-compose down

Additional Commands

Inspect volume details

docker volume inspect web-content

Output example:

[
    {
        "CreatedAt": "2025-03-30T16:42:15Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/web-content/_data",
        "Name": "web-content",
        "Options": {},
        "Scope": "local"
    }
]

List all volumes

docker volume ls

Remove all unused volumes

docker volume prune

Backup a volume

# Create a tarball backup of a volume
docker run --rm -v web-content:/source -v $(pwd):/backup alpine tar -czvf /backup/web-content-backup.tar.gz -C /source .

Restore a volume from backup

# Restore from a backup tarball
docker run --rm -v web-content:/target -v $(pwd):/backup alpine sh -c "tar -xzvf /backup/web-content-backup.tar.gz -C /target"