brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Guide: back up and restore a Docker volume with one throwaway container

back up and restore a Docker volume with one throwaway container

Guide docker

10 steps 5 min read

What you need

Docker Engine, a named volume (I use myapp_data as a stand-in for yours) and a folder on the host for the backup file. The commands use the ubuntu image, as the Docker docs do. If your container uses a bind mount like an Unraid appdata folder, this is not a volume: back up that folder with your normal file backup instead.

Step 1 of 10: Find the volume

List the volumes and check which one your container uses.

docker volume ls
docker volume inspect myapp_data

The inspect output shows where Docker keeps the volume. Do not copy files from that path by hand; let a container do it.

Step 2 of 10: Stop what writes to it

A database that is writing while you tar the files can give you a backup that does not restore. The safe variant is to stop the container first.

docker stop myapp

For a database, a proper dump with the database’s own tool is better than copying its files. Treat the tar backup as the second layer.

Animation: listing volumes, stopping a container and running the backup command
The three commands that matter

Step 3 of 10: Make the backup

Start a throwaway container that mounts the volume and a host folder, and tars the contents. The --rm flag removes the container afterwards.

docker run --rm -v myapp_data:/data -v "$(pwd)":/backup ubuntu tar czf /backup/myapp_data.tar.gz -C /data .

The Docker docs show the same idea with --volumes-from a container and a tar of its data folder. Mounting the named volume directly with -v name:/path is the same volume syntax the docs use elsewhere. You now have myapp_data.tar.gz in your current folder.

Step 4 of 10: Restore into a new volume

Test the restore on a fresh volume, not on the one you are trying to protect.

docker volume create myapp_data_test
docker run --rm -v myapp_data_test:/data -v "$(pwd)":/backup ubuntu tar xzf /backup/myapp_data.tar.gz -C /data

Docker’s docs restore with tar xvf into the new container’s volume. Here I extract with -C /data so the files land at the top of the volume.

Step 5 of 10: Check it, then clean up

Look inside the test volume and compare it with the original.

docker run --rm -v myapp_data_test:/data ubuntu ls -la /data
docker volume rm myapp_data_test
docker start myapp

Only remove the test volume once you have looked. docker volume rm deletes data for good, so check the name twice. Then start your container again.

Step 6 of 10: Where it goes wrong

The usual problems: a backup taken while the app was writing, a restore that nests everything one folder deeper, and permissions that differ after extracting. If an app will not start after a restore, compare the owner of the files with the original. Keep the tar file somewhere other than the same disk, or it is not a backup. I would run this by hand once to be sure it works, and then put it in a scheduled job.

Step 7 of 10: Back up several volumes in one go

Once one volume works, a loop is enough for the rest. List the names you want and run the same throwaway container for each one. Keep the list in a file or a script so that you do not forget a volume when you add a container later.

for v in myapp_data myapp_config; do
  docker run --rm -v "$v":/data -v "$(pwd)":/backup ubuntu \
    tar czf "/backup/$v-$(date +%F).tar.gz" -C /data .
done

The date in the file name keeps old backups from being overwritten. It also means the folder fills up, so decide how many to keep and delete the rest on purpose.

Step 8 of 10: Look inside the archive before you need it

List the contents with tar and check that the paths look right and that nothing is empty:

tar tzf myapp_data-2026-11-18.tar.gz | head

Look for the top of the volume: the files should start at the root of the archive, not inside an extra folder. That is the nesting problem I mentioned earlier, and it is much easier to catch here than during a restore at a bad moment.

Step 9 of 10: Compose and bind mounts

If your container comes from a compose file, the volume name usually gets the project name in front of it. Use docker volume ls to see the real name before you run the backup. And when your data lives in a bind mount, a host folder you pointed to, the steps above do not apply at all: the data is already an ordinary folder, and your normal file backup can copy it, ideally while the container is stopped.

Step 10 of 10: Where the archive should end up

The tar file in your current folder is only the first step. A copy on the same disk does not survive a disk failure, and a copy on the same machine does not survive theft or a fire. Put it on another disk or another machine, and test a restore from that copy now and then. I would write down the restore commands next to the backup commands, because you will be in a hurry when you need the second set. A short text file in the same folder as the archives is enough, and it also records which container and which volume each archive belongs to.

Further reading

Link