0
0
Dockerdevops~5 mins

Volumes for persistent data in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run a container, its data disappears if the container stops or is removed. Volumes let you save data outside the container so it stays safe and can be reused.
When you want to save a database's data so it is not lost when the container restarts.
When you need to share files between your host computer and a running container.
When you want to keep logs or configuration files persistent across container updates.
When multiple containers need access to the same data safely.
When you want to back up or migrate container data easily.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-app:
    image: nginx:1.23
    volumes:
      - my-data:/usr/share/nginx/html
volumes:
  my-data:
    driver: local

This file defines a service named my-app using the nginx image.

The volumes section mounts a volume called my-data inside the container at /usr/share/nginx/html.

The volumes key at the bottom creates the named volume my-data using the local driver, which stores data on the host machine.

Commands
This command creates a named volume called 'my-data' on your host machine to store persistent data.
Terminal
docker volume create my-data
Expected OutputExpected
my-data
This runs an nginx container named 'my-nginx' in detached mode and mounts the 'my-data' volume inside the container at the nginx html folder.
Terminal
docker run -d --name my-nginx -v my-data:/usr/share/nginx/html nginx:1.23
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
-d - Run container in background (detached mode)
--name - Assign a name to the container
-v - Mount a volume inside the container
This lists all volumes on your host machine so you can verify 'my-data' exists.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local my-data
This shows detailed information about the 'my-data' volume, including its mount point on the host.
Terminal
docker inspect my-data
Expected OutputExpected
[ { "CreatedAt": "2024-06-01T12:00:00Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-data/_data", "Name": "my-data", "Options": {}, "Scope": "local" } ]
Key Concept

If you remember nothing else from this pattern, remember: volumes keep your container data safe and reusable outside the container lifecycle.

Common Mistakes
Not creating or specifying a volume before running the container.
Data will be stored inside the container and lost when it stops or is removed.
Always create and mount a named volume or bind mount to persist data.
Mounting a volume to the wrong container path.
The application inside the container won't find or save data where expected.
Check the container's documentation for correct paths to mount volumes.
Using anonymous volumes without names for important data.
Anonymous volumes are hard to manage and clean up, risking data loss.
Use named volumes for important persistent data.
Summary
Create a named volume to store data persistently on the host.
Run containers with the volume mounted to the correct path inside the container.
Verify volumes exist and inspect their details to manage data safely.