0
0
Dockerdevops~5 mins

Why data persistence matters in Docker - Why It Works

Choose your learning style9 modes available
Introduction
When you run applications in containers, any data created inside the container disappears when the container stops. Data persistence means saving data outside the container so it stays safe even if the container is removed or restarted.
When you want to keep your database data safe even if the database container stops or is deleted
When you run a web app that lets users upload files and you want those files to stay available
When you need to save logs or reports generated by a container for later review
When you want to share data between multiple containers running on the same host
When you want to upgrade or replace containers without losing important data
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: examplepass
      MYSQL_DATABASE: mydatabase
    volumes:
      - db-data:/var/lib/mysql
volumes:
  db-data: {}

This file defines a MySQL database container with a volume named db-data. The volume stores the database files outside the container's writable layer, so data persists even if the container is removed or recreated.

The volumes section at the bottom declares the named volume db-data. The volumes key under the service maps this volume to the container's data directory /var/lib/mysql.

Commands
This command starts the MySQL container in detached mode using the docker-compose.yml file. It creates the volume automatically if it does not exist.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "db-data" with default driver Creating my-db ... done
-d - Run containers in the background (detached mode)
This command lists all Docker volumes on the host to verify that the volume for data persistence was created.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME driver db-data
This command stops and removes the containers but keeps the volume intact, so data is not lost.
Terminal
docker-compose down
Expected OutputExpected
Stopping my-db ... done Removing my-db ... done Removing network default
Starting the container again will reuse the existing volume, so the database data remains available.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating my-db ... done
-d - Run containers in the background (detached mode)
Key Concept

If you remember nothing else from this pattern, remember: storing data outside containers keeps it safe when containers stop or are replaced.

Common Mistakes
Not using volumes and storing data only inside containers
Data will be lost when the container is removed or crashes, causing loss of important information.
Always use Docker volumes or bind mounts to save data outside the container's writable layer.
Removing volumes when stopping containers
Deleting volumes deletes all stored data, defeating persistence.
Use 'docker-compose down' without the '-v' flag to keep volumes intact.
Summary
Use Docker volumes to save data outside containers for persistence.
Start containers with volumes to keep data safe across restarts and removals.
Stopping containers without deleting volumes preserves your data.