Backup/Restore Docker Data

In this lab, you will learn how to perform Docker backups, including backing up Docker volumes, images, and configurations. You'll also learn how to restore from these backups.

Prerequisites:


Step 1: Setup a Sample Docker Environment

1.1 Create a Sample Docker Container with a Volume

Let's start by creating a container with a volume where containers store persistent data.

1.2 Verify the Running Container

To verify that your container is running:

docker ps

You should see the my_nginx container running.

1.3 Add Some Data to the Volume

Let’s create a sample file inside the container’s volume:

docker exec my_nginx sh -c "echo 'Hello, Docker Backup!' > /usr/share/nginx/html/index.html"

Now, if you visit http://srvX.lab.npnog.org.np:8080, you should see the message "Hello, Docker Backup!" in your browser.


Step 2: Back Up Docker Volumes

2.1 List Docker Volumes

Check all the Docker volumes on your system:

docker volume ls

You should see nginx_data listed.

2.2 Backup a Docker Volume

Now, let’s back up the nginx_data volume to a tarball:

docker run --rm -v nginx_data:/data -v $(pwd):/backup busybox tar cvf /backup/nginx_data_backup.tar /data

Step 3: Back Up Docker Images

3.1 Save the Docker Image

To back up the Docker image used by the container (in this case, nginx), use the docker save command:

docker save -o nginx_image_backup.tar nginx

Step 4: Back Up Docker Compose (Optional)

If you're using Docker Compose, backing up the docker-compose.yml file is essential. Simply copy the file to your backup location.

Example:

cp /path/to/docker-compose.yml /path/to/backup/

Step 5: Restore Docker Volumes and Images

5.1 Restore a Docker Volume

To restore a Docker volume from the backup tarball:

  1. Remove or stop the current container (optional if necessary):

    docker rm -f my_nginx
  2. Recreate the volume and restore the data:

    docker volume create nginx_data docker run --rm -v nginx_data:/data -v $(pwd):/backup busybox tar xvf /backup/nginx_data_backup.tar -C /data

5.2 Restore a Docker Image

To restore the Docker image from the tarball:

docker load -i nginx_image_backup.tar

Step 6: Verify the Restore

6.1 Recreate the Container

Now, recreate the my_nginx container using the restored volume and image:

docker run -d --name my_nginx -v nginx_data:/usr/share/nginx/html -p 8080:80 nginx

6.2 Check if Data is Restored

Visit http://srvX.lab.npnog.org.np:8080 again. If the backup and restore were successful, you should see the message "Hello, Docker Backup!" just as before.


Step 7: Automating Backups

7.1 Setting Up a Cron Job for Regular Backups

You can schedule backups to run automatically using cron jobs on Linux systems. To set up a daily backup of the nginx_data volume:

  1. Open the crontab editor:

    crontab -e
  2. Add the following line to schedule the backup at 2 AM every day:

    0 2 * * * docker run --rm -v nginx_data:/data -v /backup:/backup busybox tar cvf /backup/nginx_data_backup.tar /data

    This ensures that your volume data is regularly backed up without manual intervention.