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:
Let's start by creating a container with a volume where containers store persistent data.
Run a simple container using the nginx
web server, creating a volume to store data:
docker run -d --name my_nginx -v nginx_data:/usr/share/nginx/html -p 8080:80 nginx
-d
: Run the container in detached mode.--name
: Assign a name to the container (my_nginx
).-v
: Create a Docker volume (nginx_data
) and mount it to the container.-p
: Map port 8080 on your host to port 80 inside the container.To verify that your container is running:
docker ps
You should see the my_nginx
container running.
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.
Check all the Docker volumes on your system:
docker volume ls
You should see nginx_data
listed.
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
Explanation:
--rm
: Automatically remove the container after it’s done.-v nginx_data:/data
: Mount the Docker volume (nginx_data
) to /data
inside the container.-v $(pwd):/backup
: Mount the current working directory on the host to /backup
inside the container, where the tarball will be stored.busybox
: A small utility container for running commands like tar
.tar cvf
: Create a tarball (compressed file) from the /data
directory.Result:
nginx_data_backup.tar
in your current directory.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
nginx
image, saving it as nginx_image_backup.tar
.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/
To restore a Docker volume from the backup tarball:
Remove or stop the current container (optional if necessary):
docker rm -f my_nginx
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
nginx_data_backup.tar
into the nginx_data
volume.To restore the Docker image from the tarball:
docker load -i nginx_image_backup.tar
nginx
image into Docker.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
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.
You can schedule backups to run automatically using cron jobs on Linux systems. To set up a daily backup of the nginx_data
volume:
Open the crontab editor:
crontab -e
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.