Learning paths / Containers / Persistent storage

Try it: lose data, then keep it with a volume

Reading · 15 min · Module 7, lesson 3 of 525 min left in this module

Module 7 · Persistent storageLesson 3 of 5

Goal: Show that a replaced container loses its files, then prove a volume keeps them.

3:08 · captions and chapters · narrated with an AI-generated voice
Transcript

Narration uses an AI-generated voice.

[00:00] Where we're going

By the end of this video, you'll have watched a container lose its notes, and a volume keep them. It's one small test, run twice. First, the notes are written inside a container. Then, the same notes go to a volume.

[00:15] Write a note

Every container here runs a tiny Linux image, with one short command. It adds a line with the time to a notes file, then prints the file. Each new line is one run. Run it, and you get one note. The container exits, but it still exists, stopped. Now start that same container again, and show its output. Two lines. The first note is still there: same container, same layer.

[00:44] Replace the container

Now do what an update does. Remove the old container, and start a new one from the image. Same name, same image, same command. One line again. The earlier notes are gone. Then remove this one too.

Here's why. The image is read only. Each container adds its own thin, writable layer on top. Starting the same container again keeps that layer. Removing it deletes the layer, with every note inside. And a new container starts with an empty one. Nothing is copied across.

[01:21] Keep it with a volume

So keep the notes somewhere else. Create a volume called notes data. Docker prints its name back. Now run two containers that mount it at slash data. Each one is deleted as soon as it exits. The first prints one line. The second is a brand new container. And it prints two.

The notes survived because they were never in either container. They're on the volume, at slash data. Docker keeps it outside any container, and mounts it in. Containers come and go. The volume stays until you delete it.

[02:01] A folder on your machine

A bind mount works the same way, with a folder you choose. Make a folder, and mount it at slash data. This time the container only writes. Read the file straight from your machine. There it is: written by the container. It's an ordinary file, so you can open it in your editor, too.

Clean up when you're done. Delete the volume, check the list is empty, then remove the folder. Deleting a volume deletes its data. There's no undo.

[02:32] Recap

To recap. A container's own files last exactly as long as that container. A volume, or a folder you mount, lasts as long as you want it to. So anything you need to keep goes outside the container.

Here's a question to check yourself. Step two kept the note, and step three lost it. What's the difference? Step three used a new container, with a fresh layer. The image was the same. Next, volumes on ComputeSphere. I'll see you there.

You need Docker running (set up in lesson 3.1.1), and a terminal. Everything happens on your machine; nothing is pushed.

About the commands

Every container here runs alpine:3.22, a tiny Linux image, with one shell command: add a line with the time to a notes file, then print the file. Each new line is one "run". Your times will differ from the ones shown, and they are in UTC: containers use UTC unless told otherwise, so the hour may not match your clock.

On Windows, use PowerShell with Docker Desktop. Write ${PWD} where the steps say $PWD.

  1. Write a note in a container

    docker run --name notes-demo alpine:3.22 sh -c 'echo "note written at $(date +%T)" >> /notes.txt; cat /notes.txt'
    

    The container runs the command and exits. It still exists, stopped, with its writable layer.

    You should seeOne line, such as note written at 01:50:34.

  2. Start the same container again

    docker start -a notes-demo
    

    start reruns the same container, and -a shows its output. The first note is still there: same container, same layer.

    You should seeTwo lines: the first note, and a new one.

  3. Replace the container

    docker rm notes-demo
    docker run --name notes-demo alpine:3.22 sh -c 'echo "note written at $(date +%T)" >> /notes.txt; cat /notes.txt'
    docker rm notes-demo
    

    This is what an update does: remove the old container, start a new one from the image. The new one began with an empty layer.

    Check yourself

    Step 2 kept the note and step 3 lost it. What's the difference?

    You should seeOne line again. The earlier notes are gone.

  4. Create a volume

    docker volume create notes-data
    

    You should seenotes-data, printed back.

  5. Write to the volume from two containers

    docker run --rm -v notes-data:/data alpine:3.22 sh -c 'echo "note written at $(date +%T)" >> /data/notes.txt; cat /data/notes.txt'
    docker run --rm -v notes-data:/data alpine:3.22 sh -c 'echo "note written at $(date +%T)" >> /data/notes.txt; cat /data/notes.txt'
    

    --rm deletes each container as it exits, so these are two different containers. The notes survive because they're on the volume at /data, not in either container.

    You should seeOne line after the first command, two after the second.

  6. Try a bind mount

    mkdir notes-folder
    docker run --rm -v "$PWD/notes-folder:/data" alpine:3.22 sh -c 'echo "written by the container" >> /data/notes.txt'
    cat notes-folder/notes.txt
    

    Open notes-folder/notes.txt in your editor too: it's an ordinary file.

    You should seewritten by the container, read from a file on your machine.

  7. Clean up

    docker volume rm notes-data
    docker volume ls --filter name=notes
    rm -r notes-folder
    

    Deleting a volume deletes its data, and it can't be undone.

    You should seenotes-data printed by rm, and no notes-data in the list.

What you proved

The container's own filesystem lasted exactly as long as the container. The volume and the folder lasted as long as you wanted them to, because they were never inside a container.

If docker volume rm says the volume is in use

It prints volume is in use and a container ID when a container, even a stopped one, still has it mounted. Run docker ps -a to find it, remove it with docker rm -f <name>, then delete the volume again.