Bobcares

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 *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF