🎓 A1Learner
🔍
0 of 50 lessons completed

What Are Docker Volumes?

A Docker volume is a named store of data that lives outside any one container. Container data disappears with the container, and a volume fixes that.

Create a volume

This command creates a volume named g5-data.

docker volume create g5-data

Write into the volume

This command writes a line into a file inside the volume.

docker run --rm -v g5-data:/data alpine:3.20 sh -c "echo hello > /data/note.txt"

Example

docker volume create g5-data
docker run --rm -v g5-data:/data alpine:3.20 sh -c "echo hello > /data/note.txt"
docker run --rm -v g5-data:/data alpine:3.20 cat /data/note.txt

Key points

  • Volume: a named store managed by Docker, kept outside containers.
  • A container's own files disappear when the container is removed.
  • A volume survives its container, so a new container can use the same data.
  • Create a volume with docker volume create, and list volumes with docker volume ls.
  • Attach a volume with -v name:/path when the container starts.

In this example, a volume is created, a throwaway container writes a line into it, and a second throwaway container reads the line back. Both containers are gone, and the line is still there. The line proves the volume outlived its containers.

Note

A volume is the safe home for data. A container's own layer is not.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What happens to data in a volume when its container is removed?