wesupport

Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

Docker backup – Easy steps to backup and restore your containers

by | Oct 25, 2016

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?

CLICK HERE TO BACKUP YOUR DOCKER SYSTEM!

 

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

Docker backup - container list

Docker container list

 

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.

Docker backup of container

Take backup of Docker container

 

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:

 

Docker backup of container image

Docker container backup image

 

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.

Container backup image compressed file

Compressed tar file of container backup image

 

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.

 

Load the docker backup image to the host

Load the docker backup image to the host

 

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.

 

Restore docker backup image to a new container

Restore backup image to a new container

 

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

 

Docker backup data volumes

Data volumes in Docker

 

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.

 

docker backup - destination directory for container

Identify the destination directory for the 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.

 

 

Looking for a stable Docker setup?

Talk to our Docker specialists today to know how we can keep your containers top notch!

CLICK HERE FOR EXPERT SOLUTIONS!

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

13 Comments

  1. Sean Fadzai Matabire

    Very Helpful Article Keep up the good work. We need heroes like you in the world!

    Reply
    • Priya

      True

      Reply
    • Reeshma Mathews

      Thank you for your kind words, Sean 🙂

      Reply
  2. Priya

    Awesome… fantastic… fabulous… great work…
    Thank you

    Reply
    • Reeshma Mathews

      Thank you for your kind words, Priya 🙂

      Reply
  3. jonas arndt

    Hi Guys,

    How to backup a container with multiple “data-directory”?

    Thanks,

    // Jonas

    Reply
    • Reeshma Mathews

      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.

      Reply
  4. Elgine

    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.

    Reply
  5. carlos

    Nice tutorial it’s been very useful !!!

    Reply
  6. Charlie

    Bob, you care, thank you for this.

    Reply
  7. shilpa

    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

    Reply
    • Qwe Qwe

      Windows doesn’t come with ‘tar’ utility (nor ‘zip’) by default.

      Reply
  8. shilpa

    please share backup and restore volume container on windows

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Categories

Tags