Guide Docker Beginner

Persist Container Data

Prove that data in a named volume survives container removal.

1 min read

Objective

Write a file in one disposable container and read it from a different container.

Steps

docker volume create docker-lab-data
docker run --rm --name docker-lab-writer --mount type=volume,src=docker-lab-data,dst=/data busybox:1.37 sh -c 'echo lab-record > /data/record.txt'
docker ps -a --filter name=docker-lab-writer
docker run --rm --mount type=volume,src=docker-lab-data,dst=/data,readonly busybox:1.37 cat /data/record.txt

Expected Result

The writer container no longer exists. The reader still prints lab-record.

Investigate

docker volume inspect docker-lab-data

The volume belongs to the daemon’s environment. With Docker Desktop, its mountpoint is inside the Linux environment, not an ordinary Windows or macOS application folder.

Extension

Change the destination mount path and explain why the file is now visible at a different location. Discuss why a volume does not replace backups.

Cleanup

After verifying the result, remove only the practice volume:

docker volume rm docker-lab-data

Next: Compose project.


Docker Roadmap

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form