Skip to main content

Containers and Kubernetes · Beginner · 16 min

Keep container data safe with Docker volumes and bind mounts

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Docker Engine 29.10

You will keep data in a named volume, edit files through a read-only bind mount, fix a user ID mismatch, use tmpfs for scratch files, and back up and restore a volume with a throwaway container and tar.

On this page

A container's own filesystem is disposable. Anything a program writes inside it lives in a thin writable layer that disappears when you remove the container, and you remove containers all the time: to upgrade an image, to change a setting, or because docker compose down did it for you. Data you care about has to live somewhere outside that layer.

Docker gives you three places to put it:

  • Named volumes: storage that Docker creates and manages. Use them for data the app owns, such as a database's files.
  • Bind mounts: a folder on the server shown inside the container. Use them for files you edit yourself, such as a config file or a static site.
  • tmpfs mounts: memory only, gone when the container stops. Use them for scratch files you do not want written to disk.

In this tutorial you try each one with the small alpine:3.24 image, fix the most common permission problem with bind mounts, back up a volume to a compressed archive, restore it into a new volume, and learn which clean-up command can delete your data. Everything lives in ~/volumes-demo, in containers and volumes whose names start with notes-, and every volume carries the label tutorial=notes, so no command here touches anything you already have.

Step 1 · See data disappear without a volume

Start with the problem. Run a container that writes a file into its own filesystem, then remove the container:

Bash
mkdir -p ~/volumes-demo
docker run --name notes-scratch alpine:3.24 sh -c 'echo "written inside the container" > /note.txt'
docker rm notes-scratch
docker run --rm alpine:3.24 cat /note.txt

The last command starts a fresh container from the same image. The file is not there, because it only ever existed in the writable layer of the container you removed.

Now do the same with a named volume. docker volume create makes an empty volume, and --mount attaches it at /data inside the container. The --label marks it as this tutorial's, which the clean-up step relies on:

Bash
docker volume create --label tutorial=notes notes-data
docker run --rm --mount type=volume,src=notes-data,dst=/data alpine:3.24 \
  sh -c 'echo "first note" > /data/first.txt; date > /data/created-at.txt'
docker run --rm --mount type=volume,src=notes-data,dst=/data alpine:3.24 cat /data/first.txt

Both containers were removed as soon as they finished (--rm), yet the second one reads what the first one wrote.

You may also see the shorter -v notes-data:/data form in other guides. It does the same thing for volumes. This tutorial uses --mount because every option is named, and because it fails loudly on a mistake that -v hides, which you will see in a later step.

Check it worked

The first cat prints an error saying /note.txt does not exist. The second cat prints first note. If it prints an error instead, check that both commands use the same volume name, notes-data.

Step 2 · Find where a volume lives, and leave it alone

List your volumes and ask Docker about the new one:

Bash
docker volume ls
docker volume inspect notes-data
docker volume inspect --format '{{ .Mountpoint }}' notes-data

On Ubuntu the last command prints /var/lib/docker/volumes/notes-data/_data. That folder belongs to root and to Docker. Docker's documentation says that touching volume data directly, from outside a container, is unsupported and its behaviour is undefined. Editing a database's files there while it runs is a reliable way to corrupt them. Treat the path as information, not as a place to work.

When you want to look inside a volume, mount it into a short-lived container, read-only so you cannot change anything by accident:

Bash
docker run --rm --mount type=volume,src=notes-data,dst=/data,readonly alpine:3.24 ls -l /data

If you use Docker Desktop on a laptop rather than Docker Engine on a server, the Mountpoint path is inside the virtual machine that Desktop runs, so it does not exist on your own disk at all. The container method works in both places.

Check it worked

docker volume ls shows a row with driver local and name notes-data. The ls -l /data output lists first.txt and created-at.txt.

Step 3 · Share a folder you edit with a read-only bind mount

A bind mount shows a folder from the server inside the container. Changes on either side are visible on the other at once, which makes it the right choice for files you edit with your own tools.

Create a small site folder and mount it read-only:

Bash
mkdir -p ~/volumes-demo/site
cat > ~/volumes-demo/site/index.html <<'EOF'
<h1>Version 1</h1>
EOF
docker run --rm --mount type=bind,src="$HOME/volumes-demo/site",dst=/site,readonly alpine:3.24 \
  sh -c 'cat /site/index.html; touch /site/extra.txt'

The container can read the file but not write to the folder. By default a bind mount is writable, and a process in the container can then create, change and delete files on your server, so add readonly whenever the container only needs to read.

Edit the file on the server and run the container again. It sees the new content without any rebuild:

Bash
cat > ~/volumes-demo/site/index.html <<'EOF'
<h1>Version 2</h1>
EOF
docker run --rm --mount type=bind,src="$HOME/volumes-demo/site",dst=/site,readonly alpine:3.24 cat /site/index.html

Now the mistake --mount protects you from. Mistype the source folder:

Bash
docker run --rm --mount type=bind,src="$HOME/volumes-demo/sitte",dst=/site,readonly alpine:3.24 ls /site

Docker refuses to start the container and says the bind source path does not exist. With -v "$HOME/volumes-demo/sitte:/site" Docker would instead create an empty sitte folder and start the container with nothing in /site, and your app would run without its files and without an error.

One more thing to know: a bind mount hides whatever the image had at that path. If you bind a folder over /etc/nginx, the image's own config files are no longer visible inside the container.

Check it worked

The first run prints <h1>Version 1</h1> followed by an error that ends in Read-only file system. The second run prints <h1>Version 2</h1>. The mistyped run prints an error and no file list, and ls ~/volumes-demo shows no sitte folder.

Step 4 · Fix permission mismatches with matching user IDs

Linux decides who owns a file by number, the user ID (UID), not by name. A container and your server agree on the numbers but not on the names, and that is where bind mount permission problems come from.

Find your own UID and group ID, then write a file into a bind mount, first as the container's default user and then as you:

Bash
id -u
id -g
mkdir -p ~/volumes-demo/out
docker run --rm --mount type=bind,src="$HOME/volumes-demo/out",dst=/out alpine:3.24 touch /out/as-root.txt
docker run --rm --user "$(id -u):$(id -g)" --mount type=bind,src="$HOME/volumes-demo/out",dst=/out alpine:3.24 touch /out/as-me.txt
ls -ln ~/volumes-demo/out

The alpine image runs as root, UID 0, so the first file belongs to root on your server too, and you cannot edit it without sudo. The --user flag runs the container's process with your numbers, so the second file is yours.

The opposite problem shows up with images that run as a fixed non-root user. Pretend the image runs as UID 1234:

Bash
docker run --rm --user 1234:1234 --mount type=bind,src="$HOME/volumes-demo/out",dst=/out alpine:3.24 touch /out/as-1234.txt

It fails, because the folder belongs to you and UID 1234 has no write permission there. You have two fixes:

  • Run the container with your own UID using --user, when the image allows it.
  • Give the folder to the UID the image expects, with sudo chown 1234:1234 <folder>. Replace 1234 and the folder with your real values.

To find which user an image runs as, check its config. An empty answer means root:

Bash
docker image inspect --format '{{ .Config.User }}' alpine:3.24

Named volumes mostly avoid this problem. When Docker mounts a new, empty volume over a folder that already has files in the image, it copies those files into the volume first, unless you add the volume-nocopy option. The volume starts with the image's owner and permissions for that folder, so an image that runs as a non-root user can usually write to it without any chown.

Check it worked

ls -ln shows as-root.txt with owner 0 0 and as-me.txt with the two numbers that id -u and id -g printed. The UID 1234 run prints Permission denied.

Step 5 · Keep scratch files in memory with tmpfs

Some files should never reach the disk: temporary uploads, session files, or a cache you are happy to lose. A tmpfs mount keeps them in memory and throws them away when the container stops.

Bash
docker run --rm --mount type=tmpfs,dst=/scratch,tmpfs-size=16m alpine:3.24 \
  sh -c 'df -h /scratch; echo "temporary" > /scratch/tmp.txt; cat /scratch/tmp.txt'

Always set tmpfs-size. Without it the limit is half of the server's memory. Everything written there counts as memory: if the container has a memory limit (--memory), filling the tmpfs can get it killed; without one it eats into the server's memory. On a small virtual server with 1 or 2 GB of memory, keep tmpfs mounts to a few megabytes. tmpfs works only with Docker on Linux, and one container cannot share its tmpfs with another.

Check it worked

The df -h line shows filesystem tmpfs with a size of about 16 MB mounted on /scratch, and the next line prints temporary.

Step 6 · Back up a volume with a throwaway container and tar

To back up a volume, start a short-lived container that mounts the volume read-only and a backup folder from your server, and let tar inside it write a compressed archive.

First make sure nothing is writing to the volume. A database, for example, can leave half-written files in a copy taken while it runs. This command lists containers, running or stopped, that use the volume:

Bash
docker ps -a --filter volume=notes-data

Stop any running container it lists with docker stop <name> before you continue. For PostgreSQL, a logical dump is safer still: see Back up and restore PostgreSQL with pg_dump.

Backups often hold private data, so the archive is created with umask 077, which makes it readable by its owner only from the moment it exists:

Bash
mkdir -p ~/volumes-demo/backups
docker run --rm \
  --mount type=volume,src=notes-data,dst=/data,readonly \
  --mount type=bind,src="$HOME/volumes-demo/backups",dst=/backup \
  alpine:3.24 sh -c 'umask 077 && tar -czf /backup/notes-data.tar.gz -C /data .'
ls -l ~/volumes-demo/backups
sudo tar -tzvf ~/volumes-demo/backups/notes-data.tar.gz

-C /data . stores paths relative to the volume, so the archive can be unpacked into any volume later. The container runs as root, so the archive belongs to root and you need sudo to read it. For a real backup, add the date to the file name (for example notes-data-$(date +%F).tar.gz) and copy the archive off the server: a backup on the same disk is lost with the disk. The restic tutorial in Next steps shows one way to do that.

If you pay for data by the gigabyte, as on many prepaid plans, the compressed archive is what you move, so it is worth checking its size with ls -lh before you download it.

Check it worked

ls -l shows notes-data.tar.gz with permissions -rw------- and owner root. The tar -tzvf listing shows ./first.txt and ./created-at.txt.

Step 7 · Restore the backup into a new volume

Restoring is the same idea in reverse: an empty volume, the archive mounted read-only, and tar -x. Restore into a new volume rather than over the old one, so a bad archive cannot destroy the only good copy.

Bash
docker volume create --label tutorial=notes notes-restored
docker run --rm \
  --mount type=volume,src=notes-restored,dst=/data \
  --mount type=bind,src="$HOME/volumes-demo/backups",dst=/backup,readonly \
  alpine:3.24 tar -xzf /backup/notes-data.tar.gz -C /data
docker run --rm --mount type=volume,src=notes-restored,dst=/data,readonly alpine:3.24 \
  sh -c 'ls -l /data; cat /data/first.txt'

Once you have checked the restored data, point your app at the new volume (in Compose, change the volume name under volumes:), or keep both until you are sure. A backup you have never restored is a guess, so repeat this check from time to time.

Check it worked

The listing shows first.txt and created-at.txt, and the last line prints first note, the text you wrote in the first step.

Step 8 · List, prune and clean up without losing data

Docker never deletes a volume just because no container uses it, so unused volumes pile up and fill the disk. Read this before you clean up.

Warning: docker volume prune deletes data permanently, and there is no undo. By default it removes only unused anonymous volumes, the ones with long random names that -v /path or an image's VOLUME line creates. With --all it also removes every named volume that no container currently uses. That includes a database volume whose container you removed yesterday to upgrade it.

See what is unused before you delete anything. On a real server this list can include volumes from other apps, so read every line:

Bash
docker volume ls --filter dangling=true

dangling=true lists every volume no container uses, named or anonymous, so it shows what prune --all would remove. For anything you care about, delete by name instead of pruning.

Filters narrow both commands to volumes you chose. This tutorial's volumes all carry tutorial=notes, so you can list just them, then prune just them:

Bash
docker volume create --label tutorial=notes --label keep=true notes-keep
docker volume ls --filter label=tutorial=notes --filter dangling=true
docker volume prune --all --filter label=tutorial=notes --filter 'label!=keep'

The prune command asks for confirmation before it deletes anything. Answer y: with both filters it removes only notes-data and notes-restored, the unused volumes with the tutorial label, and skips notes-keep because it has a keep label. On your own servers the same idea protects important volumes: create them with --label keep=true, and a clean-up such as docker volume prune --all --filter 'label!=keep' leaves them alone. That form still removes every other unused named volume, so run the dangling=true list first. Clean up Docker disk usage safely covers it in full.

To finish, remove the protected volume by name, then the demo folder. The sudo rm -rf deletes ~/volumes-demo and the backup inside it, so copy the archive elsewhere first if you want to keep it:

Bash
docker volume rm notes-keep
sudo rm -rf ~/volumes-demo

Check it worked

docker volume ls --filter name=notes- prints only its header line, and ls ~/volumes-demo reports that the folder does not exist.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.