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:
...
Honestly, I could not find a simple way to backup/restore Docker named volumes:
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.
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?
@MedMerahi shared a post that was simple enough for me to understand. I'd like to share the steps I took:
docker volume ls
For me wordpress_wordpress
is one of the listed volumes which I want to back up.
docker run --rm \
--mount source=<volume-name>,target=<target> \
-v $(pwd):/backup \
busybox \
tar -czvf /backup/<backup-filename>.tar.gz <target>
<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:
docker run --rm \
--mount source=wordpress_wordpress,target=/mount_point \
-v $(pwd):/backup \
busybox \
tar -czvf /backup/wordpress_wordpress.tar.gz /mount_point
It created the wordpress_wordpress.tar.gz
file inside my current directory.
scp /path/to/backupfile user@external-server:/path/to/destination
For me, I ran the equivalent of the above command on my local laptop to fetch the backup file from server:
scp user@88.88.88.8:/home/user/wordpress_wordpress.tar.gz .
docker run --rm \
--mount source=<volume-name>,target=<target> \
-v $(pwd):/backup \
busybox \
tar -xzvf /backup/<backup-filename>.tar.gz -C /
<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:
docker run --rm \
--mount source=wordpress_wordpress,target=/mount_point \
-v $(pwd):/backup \
busybox \
tar -xzvf /backup/wordpress_wordpress.tar.gz -C /
I took the risk and restored the backup file to test if the process actually works. It did work great :)