Persistent Data with Docker Volumes
Containers are ephemeral by design. When a container crashes, restarts, or is destroyed, all the data written inside it dies with it. While this is great for stateless applications like web APIs, it is a disaster if you are running a database like PostgreSQL or MongoDB. To safely persist database records, user uploads, and application state, you must use Docker Volumes.
- Why containers lose data and how the container writable layer works.
- The critical difference between Bind Mounts and Docker Volumes.
- How to create, attach, and manage Docker Volumes via the CLI.
- How to implement named volumes inside a
docker-compose.ymlfile.
1. The Problem with Container Storage
Every Docker container consists of read-only image layers and a single, thin "writable layer" on top. When your application writes a file or inserts a database row, it goes into this thin layer. The moment you run docker rm, that writable layer is wiped out permanently.
To solve this, Docker offers external storage options that bypass the container's lifecycle entirely.
| Storage Type | Where is it stored? | Best Use Case |
|---|---|---|
| Writable Layer | Inside the container | Temporary caches, logs |
| Bind Mounts | Specific host OS folder (e.g. C:\my-code) |
Live code reloading during local dev |
| Docker Volumes | Managed securely by Docker in a hidden host area | Databases, permanent user uploads |
2. Practical Implementation & Code
Let's look at how to spin up a PostgreSQL database container that will not lose your data if it crashes.
Creating and Attaching a Volume via CLI
Example 1: Running Postgres with a Named Volume
# Step 1: Create the volume
docker volume create pg_data
# Step 2: Run the container and mount the volume
docker run -d \
--name my_postgres \
-e POSTGRES_PASSWORD=secret \
-v pg_data:/var/lib/postgresql/data \
postgres:15
Expected output
pg_data 1a2b3c4d5e6f7g8h9i0j...
-v flag uses the format volume_name:container_path. The path /var/lib/postgresql/data is exactly where Postgres writes its database files internally. By mounting a volume there, Docker maps that internal folder to safe, external storage.Doing it the Modern Way: Docker Compose
In the real world, you will almost always define volumes declaratively inside a Compose file rather than typing long CLI commands.
Example 2: docker-compose.yml configuration
version: '3.8'
services:
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret
volumes:
- pg_data:/var/lib/postgresql/data
volumes:
pg_data: # This tells Compose to create a Docker-managed volume
3. Architecture / Workflow Visualized
When you use a Docker Volume, you are telling the Docker Engine to carve out a protected piece of your host machine's hard drive and map it directly into the container.
Docker Volume Architecture
Even if the PostgreSQL container is completely deleted, the data lives on securely within the Docker Managed Volume on the host file system.
4. Step-by-Step Execution Breakdown
Here is exactly what the Docker Engine does behind the scenes when you run a container with a volume.
pg_data exists. If it does not, Docker provisions a secure folder on the host machine./var/lib/postgresql/data) and effectively "glues" it to the external volume.-v ./my-folder:/data), you may run into severe permission issues between your host OS user and the internal container user, especially on Linux environments. Stick to Docker Volumes for databases.5. Hands-On Practice
Test your understanding of Docker Volumes by running through these scenarios.
Create a volume using docker volume create my_test_vol. Next, use the inspection command to find out exactly where Docker is storing this volume on your hard drive.
Hint: Use docker volume inspect my_test_vol and look for the "Mountpoint" property.
Run a Redis container and mount a volume to /data. Exec into the container, save a key-value pair, kill and remove the container, then start a new one with the same volume. Check if your key is still there.
Expected result: The new container should immediately have access to the key you saved in the old container.
Docker Volumes are isolated. How do you back up the data inside a volume to a `.tar` file on your host machine so you can move it to another server?
Hint: You will need to run a temporary busybox or alpine container that mounts both the database volume AND a bind mount to your local desktop, then use the tar command to archive the folder.
6. Interview Questions
If you are applying for backend, DevOps, or system architecture roles, you will be asked about container persistence.
Answer: A Bind Mount maps an explicit, user-specified file or directory from the host OS into the container. It is highly dependent on the host's directory structure and OS permissions, making it great for local development but poor for production. A Docker Volume is a storage area entirely managed by Docker within its own internal host directory (usually /var/lib/docker/volumes). It is completely decoupled from the host's core file system, making it cross-platform safe, easier to back up, and the absolute standard for persisting database files in production.
docker rm -f, does it delete the attached volume?
Answer: No. Docker intentionally decouples the lifecycle of a volume from the lifecycle of a container. To delete a volume, you must explicitly run docker volume rm [volume_name] or use docker system prune --volumes.
Conclusion
In my 15+ years of software architecture, the number one mistake I see developers make when transitioning to microservices is trusting local container storage. Remember: containers are meant to be cattle, not pets. You should be able to kill and restart them at any moment without fear. By strictly using Docker Volumes for databases, caches, and file uploads, you decouple your precious state from your disposable compute logic.
Official Documentation
Upgradix by Shrash Studio – Learn Technology. Build Your Future.