301 — Docker Volumes and Data Persistence

Master data persistence in Docker using volumes, bind mounts, and tmpfs. Learn to manage stateful applications and share data between containers.

Learning Objectives

1
Understand container filesystem vs persistent storage
2
Create and manage Docker volumes
3
Use bind mounts for development workflows
4
Share data between multiple containers
5
Back up and restore volume data
Step 1

Understanding container ephemeral storage

See how container data disappears when containers are removed.

Commands to Run

docker run --name temp-container ubuntu bash -c 'echo "Important data" > /data.txt && cat /data.txt'
docker start temp-container
docker exec temp-container cat /data.txt
docker rm -f temp-container
docker run --name temp-container ubuntu cat /data.txt

What This Does

Container filesystems are ephemeral. Data created inside a container is lost when the container is removed. Each new container starts fresh from the image.

Expected Outcome

First container shows the data. After removal, new container with same name can't find /data.txt - the file is gone.

Pro Tips

  • 1
    Container layers are temporary by default
  • 2
    Data persists while container exists (even when stopped)
  • 3
    Removing the container destroys its filesystem layer
  • 4
    Use volumes for data that must survive container lifecycle

Common Mistakes to Avoid

  • āš ļøAssuming data inside containers is permanent
  • āš ļøLosing important data by removing containers
  • āš ļøNot planning for data persistence from the start

All Steps (0 / 14 completed)