Disasters cannot be predicted or prevented, but you can always quickly bounce back from them if you maintain adequate backup plans for your business.
That makes data backups indispensable for any online business. By reducing the time to restore a crashed business, backups help to save businesses and minimize the losses.
In Docker systems, containers are used to host applications or services. Today, we’ll see how to perform docker backup and restore functions for containers.
In Docker, data is categorized into two – docker images for creating containers and the container data. The container data may be stored in containers or data volumes.
Are your Docker containers insured?
How to backup Docker containers
1. Before backing up a container, you need to identify its container ID. To know the container ID of a Docker instance, you can list the containers in that system.
The containers in a Docker machine can be listed using the command ‘docker ps’.
2. To backup a container in this list, use the ‘docker commit’ command with the following syntax:
docker commit -p container-ID backup-name
The container ID can be obtained from the ‘docker ps’ list and the ‘backup-name’ can be chosen based on your backup policy.
This snippet shows the docker backup of container ‘679facd4ee38’ (WordPress container), being taken in the name ‘backup-01’.
3. The backup image of the container, also known as ‘snapshot’, would be created with the name ‘backup-01’ and will be listed in the ‘docker images’ list:
This image of the container can be used to restore it on a later point and retrieve the state of the container to that moment this snapshot was taken.
4. It is not advisable to store the backups in the same Docker host machine as the container, as a hardware crash in it can tamper with the backups too.
So we usually convert these backup images to compressed formats such as ‘tar’ files and copy them over to an external server.
The ‘tar’ file for the backup image can be generated using the command:
docker save -o backup-name.tar backup-name
5. The tar file would be generated as shown, which can be copied over to external locations using ‘rsync’ or ‘scp’ tools.
Tools such as docker-backup can also be configured to take docker backup of containers. At Bobcares, we use custom scripts to perform backups.
The docker backup scripts are compiled based on parameters such as backup frequency, storage location, priority and importance of the container, etc.
[ Running a Docker infrastructure doesn’t have to be hard, or costly. Get world class Docker management services at affordable pricing. ]
How to restore Docker containers
1. To restore the Docker container from a backup, first make sure that the backup image is present in the host machine.
If not, load the backup images using ‘docker load‘ command. Confirm that the image is present in the server using ‘docker images‘ command.
2. Once the backup images are listed in the Docker host, you can restore the container by using ‘docker run’ command and specifying the backup image.
For instance, a new container called ‘new-wp1’ was created using the backup ‘backup-01’ in our demo docker server.
How to backup Docker data volumes
Data stored inside the containers is not persistent, and will be lost upon reboot. As a result, many Docker machines now use data volumes.
Data volumes are directories that can store data outside Docker containers. This data can be shared among containers and is persistent.
The data volumes are usually stored in the folder ‘/var/lib/docker/volumes’ and can be listed using the command ‘docker volume ls’.
To backup a container whose data is stored in data volumes, the command to run is:
docker run --rm --volumes-from container-to-backup -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /data-directory
The parameters to give are:
container-to-backup - provide the name of the container which you need to backup $(pwd):/backup - you can replace '/backup' with your backup folder backup.tar - you can give your backup file name data-directory - give the path to the destination folder for the container.
To know this data-directory (data volume location) of a container, use the command ‘docker inspect container-name‘.
You will get a section called ‘Mounts‘. The parameter ‘Destination‘ in that section would give the directory for that container.
We executed this command to generate a backup of the container ‘ifs_wordpress_1’ into ‘backup.tar’ file successfully:
docker run –rm –volumes-from ifs_wordpress_1 -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /var/www/html
This command created a new container from the existing container and its data volume was backed up to the file ‘backup.tar’ inside backup directory.
The newly created container would be deleted and you’d be left with the backup file for the container.
root@demo:~# ls -lah backup.tar -rw-r--r-- 1 root root 8.6G Oct 25 14:46 backup.tar
This backup files were later copied over to external server for secure storage, using our custom scripts for regular backups.
[ Never let your business be affected by container crashes! Our Docker experts take care of your infrastructure and promptly safeguards the container data. ]
How to restore Docker data volumes
To restore a container using the backup of data volumes taken, first create a new container by providing data volume and container names:
docker run -v /data-directory --name new-container-name ubuntu /bin/bash
Next step is to untar the backup file created, to the new container`s data volume:
docker run --rm --volumes-from new-container-name -v $(pwd):/backup ubuntu bash -c "cd /data-directory && tar xvf /backup/backup.tar --strip 1"
You’ll get a new container with the data restored from the backup. It is also possible to restore the data to the existing container.
Points to note..
Today we saw how to perform docker backup and restore operations. Here are a few points to keep in mind during backups.
1. Backups should be properly named for identification and easy restore.
2. Backups should be rotated regularly or moved to another storage to avoid ‘disk full’ errors.
3. Backups should include all relevant information such as the registry data and config files too.
4. Backups should be routinely tested and verified for adequacy.
5. Backup restore should be performed with utmost caution, or else you may end up destroying containers wrongly.
Bobcares helps businesses minimize downtime with our backup management services, which range from formulating the backup plan to restoring the data within no time.
If you’d like to know how to manage your Docker infrastructure resources get the best out of them for your business, we’d be happy to talk to you.
Very Helpful Article Keep up the good work. We need heroes like you in the world!
True
Thank you for your kind words, Sean 🙂
Awesome… fantastic… fabulous… great work…
Thank you
Thank you for your kind words, Priya 🙂
Hi Guys,
How to backup a container with multiple “data-directory”?
Thanks,
// Jonas
Jonas,
It is also possible to backup data volumes. Please submit your details at https://bobcares.com/contact-us/ and our docker experts would configure the backups for you.
after running:
docker run –rm –volumes-from new-container-name -v $(pwd):/backup ubuntu bash -c “cd /data-directory && tar xvf /backup/backup.tar –strip 1”
I wasn’t able to run/start my container.
I’m using jenkins as my base image.
Nice tutorial it’s been very useful !!!
Bob, you care, thank you for this.
Hi,
i have an issue when we backup windows container volume it gives an error:
PS C:\WINDOWS\system32> docker run –volumes-from volcontainer -v C:/backup myservercore tar -cvf c:/backup/backup.tar /c:/wincorevolume
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: container 83cddcbc17e458abbd2bf97ca788ad8d664e3a3b72bca3301f2a9a9abc12485a encountered an error during CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2) extra info: {“CommandLine”:”tar -cvf c:/backup/backup.tar /c:/wincorevolume”,”WorkingDirectory”:”C:\\”,”CreateStdInPipe”:true,”CreateStdOutPipe”:true,”CreateStdErrPipe”:true,”ConsoleSize”:[0,0]}.
PS C:\WINDOWS\system32>
Can u please give me suggestion how to backup volume
Windows doesn’t come with ‘tar’ utility (nor ‘zip’) by default.
please share backup and restore volume container on windows