Home > Mobile >  Backup /var/lib/docker without images?
Backup /var/lib/docker without images?

Time:05-20

I want to make a backup of all my containers and volumes, so the easiest way would be to copy /var/lib/docker to another location.

However this directory also includes all the images, and I don't want to include them since they all can easily be re-downloaded from public sources.

So how can I copy this directory while excluding the images?

CodePudding user response:

You have to differentiate between container backup and vol backup:

Backing up a container, that is, its configurations like labels, envs, etc.

You do that by committing the container as an image:

$ docker container commit <container-name/id> <name-of-new-image>

Better give it also some metainfo:

$ docker container ...  -m "very important container config state" -a "John Doe"

Backing up a volume

Let's say the volume of interest <my-vol> is bound to a container <other-container> - which may have been created like: docker container run -v /my-vol other-container ...

So first you have to bind the volume also to a newly created temporary container with the --volumes-from flag. With the -v option you mount a local path (of the host) into the container:

$ docker container run -rm --volumes-from <other-container> \
-v <dir/on/host>:<mountpath/in/container> \
<ubuntu/centos/whatever-base-image> tar cvf <mountpath/in/container>/backup.tar /<my-vol>

After completing the command the container stops and with that it will also be deleted because of the -rm option.

Whith all that the steps are:

  • bind the volume to a temp container
  • mount a hostpath into the container
  • make a tarbal (or whatever kind of backup) of the volume in the container
  • container stops and is deleted after the backup command has finished
  • the backup tarbal is left on the mounted dir of the container host.

see also: https://docs.docker.com/storage/volumes/


Shell Command

.. the other - not recommended - way would be to do it just with os level commands:

shopt -s extglob
cp -r var/lib/docker/!(image) your/path/backup

For that you have to stop all involved containers to prevent read/write issues.

  • Related