I have this Docker compose file with named volumes:
version: '3'
services:
db:
image: mysql:8.0
container_name: db
volumes:
- dbdata:/var/lib/mysql
wordpress:
image: wordpress:5.1.1-fpm-alpine
container_name: wordpress
volumes:
- wordpress:/var/www/html
webserver:
image: nginx:1.15.12-alpine
container_name: webserver
volumes:
- wordpress:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
- certbot-etc:/etc/letsencrypt
...
certbot:
image: certbot/certbot
container_name: certbot
volumes:
- certbot-etc:/etc/letsencrypt
- wordpress:/var/www/html
...
volumes:
certbot-etc:
wordpress:
dbdata:
...
Simple backup/restore of named volumes
Honestly, I could not find a simple way to backup/restore Docker named volumes:
- https://docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes
- https://github.com/muesli/docker-backup
- https://github.com/offen/docker-volume-backup
- https://stackoverflow.com/a/39125414/3405291
- …
Some look outdated. Some look confusing, complex and prone to mistakes. I’m looking for a simple way.
There is possibility that it’s actually simple, however I don’t understand it. I appreciate if you help me understand.
Simple?
I know that when Docker creates volumes, the contents of the volume are stored in a directory on the host filesystem, /var/lib/docker/volumes/
, that’s managed by Docker.
I was thinking of simply creating a zipped file out of /var/lib/docker/volumes/
and call it a day. But there might be a reason that nobody is doing that, right?
2
Answers
@MedMerahi shared a post that was simple enough for me to understand. I'd like to share the steps I took:
Step 1: Identify the Volume
For me
wordpress_wordpress
is one of the listed volumes which I want to back up.Step 2: Create a Backup
<volume-name>
with the name of the volume you want to back up<target>
with the mount point inside the docker container<backup-filename>
with a name for the backup fileI intend to back up the
wordpress_wordpress
volume, so I ran:It created the
wordpress_wordpress.tar.gz
file inside my current directory.Step 3: Move the Backup File to a Safe Place
For me, I ran the equivalent of the above command on my local laptop to fetch the backup file from server:
Step 4: Restore the Volume
<volume-name>
with the name of the volume you want to back up<target>
with the mount point inside the docker container<backup-filename>
with a name for the backup fileMy volume name is
wordpress_wordpress
so, I ran:I took the risk and restored the backup file to test if the process actually works. It did work great :)
below article contains detailed step by step how to backup and restore docker volumes
https://info-spot.net/backup-restore-docker-volumes/
good luck