Docker Tutorial — Part 4: Volumes & Data Persistence

By default, Docker containers are temporary. If a container is deleted — all its data is gone. This is dangerous for databases and real applications.

Why Data Persistence Matters

What is a Docker Volume?

A Docker volume is a storage location managed by Docker outside the container lifecycle.

Create a Volume

docker volume create mydata

List Volumes

docker volume ls

Use Volume in a Container


docker run -d \
-v mydata:/data \
--name test-container \
nginx

Now anything stored in /data will survive container deletion.

Bind Mount (Host Directory)

Bind mounts directly map a host folder. Useful during development.


docker run -d \
-v $(pwd):/app \
--name dev-container \
node

Volume vs Bind Mount

Inspect Volume

docker volume inspect mydata

Volumes are a **must** for databases, backend apps, and production workloads.

Next: Docker Networking →
Disclaimer:
Never store critical production data without volumes.