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 ↗

Tip: back up a Docker volume with a throwaway tar container

Docker
~/tips/tip-docker-volume-backup

docker run --rm \

Archives the contents of a container's volume into a tar file on the host.

Docker volumes live in a directory managed by Docker, so there is no obvious file to copy. The docs show a way around that.

Start a temporary container that mounts the volume and a host folder, and let it write a tar archive.

docker run --rm \
  --volumes-from mycontainer \
  -v "$(pwd)":/backup \
  ubuntu tar cvf /backup/backup.tar /data

Here /data is the path where mycontainer mounts the volume. --volumes-from gives the helper the same mounts, and the current directory shows up as /backup. The --rm removes only the helper, not your volume. To restore, you do it the other way round and extract the archive into the target volume.

If the volume is a named one, you can mount it directly in the helper with -v myvolume:/data instead of using --volumes-from. Same result, and you do not need the original container to exist at all.

The caveat is consistency. A database that is writing while you tar its files can give you a broken backup. Stop the container first, or use the database’s own dump tool. I would also test one restore before I trust any of this.

Further reading: Volumes: back up, restore, or migrate data volumes, On this site: browse and upload files inside a Docker volume from Portainer, On this site: run docker system df before you prune anything, On this site: Guide: back up and restore a Docker volume with one throwaway container

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link