skip to Main Content

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:

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


  1. Chosen as BEST ANSWER

    @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

    docker volume ls
    

    For me wordpress_wordpress is one of the listed volumes which I want to back up.

    Step 2: Create a Backup

    docker run --rm 
    --mount source=<volume-name>,target=<target> 
    -v $(pwd):/backup 
    busybox 
    tar -czvf /backup/<backup-filename>.tar.gz <target>
    
    • Replace <volume-name> with the name of the volume you want to back up
    • Replace <target> with the mount point inside the docker container
    • Replace <backup-filename> with a name for the backup file

    I 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.

    Step 3: Move the Backup File to a Safe Place

    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 [email protected]:/home/user/wordpress_wordpress.tar.gz .
    

    Step 4: Restore the Volume

    docker run --rm 
    --mount source=<volume-name>,target=<target> 
    -v $(pwd):/backup 
    busybox 
    tar -xzvf /backup/<backup-filename>.tar.gz -C /
    
    • Replace <volume-name> with the name of the volume you want to back up
    • Replace <target> with the mount point inside the docker container
    • Replace <backup-filename> with a name for the backup file

    My 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 :)


  2. below article contains detailed step by step how to backup and restore docker volumes

    https://info-spot.net/backup-restore-docker-volumes/

    good luck

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search