Docker Day 7: Persist Data with Volumes So It Survives Container Deletion
This guide explains why container file systems are transient, compares three Docker storage methods, and demonstrates how to use named volumes—both via the CLI and docker‑compose—to ensure data remains intact after removing containers, with a MySQL persistence example.
Problem: container removal deletes data
When a container is removed, any files written inside it disappear because the container's filesystem is temporary. Volumes provide a persistent data store on the host.
Storage options
Volumes – Docker‑managed persistent data. Command: docker volume create Bind mounts – Map a host directory into the container (useful for development). Command: -v /host:/container tmpfs – In‑memory storage for temporary sensitive data. Command:
--tmpfs /tmpVolumes (recommended)
Creating and managing
# Create a volume
docker volume create mydata
# List all volumes
docker volume ls
# Inspect a volume
docker volume inspect mydata
# Remove unused volumes
docker volume prune
# Remove a specific volume
docker volume rm mydataUsing a volume in a container
# Method 1: specify at run time
docker run -d \
-v mydata:/var/lib/postgresql/data \
postgres:16-alpine
# Method 2: docker‑compose.yml
services:
db:
image: postgres:16-alpine
volumes:
- mydata:/var/lib/postgresql/data
volumes:
mydata:Anonymous vs. named volumes
# Anonymous volume (removed with container)
volumes:
- /var/lib/postgresql/data
# Named volume (recommended)
volumes:
- pg_data:/var/lib/postgresql/data
volumes:
pg_data:Bind mounts (development and debugging)
Map a host directory directly into the container so changes are reflected instantly.
# Format: -v host_path:container_path
docker run -it \
-v $(pwd)/src:/app/src \
-v $(pwd)/data:/app/data \
python:3.12-slim bashdocker‑compose configuration
services:
api:
build: .
volumes:
- postgres_data:/var/lib/postgresql/data # named volume for persistence
- ./app:/app # bind mount for live code updates
- ./config:/app/config:ro # read‑only mount
volumes:
postgres_data:Practical example: MySQL data persistence
Without a volume – data is lost when the container is removed.
docker run -d \
--name mysql \
-e MYSQL_ROOT_PASSWORD=secret \
mysql:8.0
# Simulate data loss
docker rm -f mysql # all data goneWith a named volume – data survives container removal.
services:
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: app
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data: docker compose up -d
# create tables, insert data
docker compose down
docker compose up -d
# data is still presentInspecting a volume
# Show the host path of a volume
docker volume inspect mysql_data
# Example output includes:
# {
# "Mountpoint": "/var/lib/docker/volumes/mysql_data/_data",
# "Name": "mysql_data",
# ...
# }Summary
Volumes : Docker‑managed persistent storage (recommended).
Bind mounts : Map host directories for real‑time development sync.
Removing a container does not delete data stored in a volume.
Use named volumes in production; avoid anonymous volumes or bind mounts for persistent data.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
