How to backup and restore Docker containers

25

The post outlines the steps to take the backup (snapshot) of docker Container and restore it. Please note that this post mainly discusses committing a container as an image. This works on the container that does not use data volume. For containers with data volume, backup of the data volume must be taken separately.

Taking backup of Docker Container

  1. Commit the required container as an image
    # docker commit -p [container-id] backup01
    sha256:89682d4xxxxxx

    Now a new image backup01 will be created. Kindly note that this will not cover the data volume. You need to take the backup of data-volume (if any) separately.

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”. Location mentioned in “Source” is the data volume. You may directly backup this folder(here /site) to get backup of data volume.

"Mounts": [
{
"Source": "/site",
"Destination": "/usr/xx/xxx/xxxs",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
  1. You can save the image backup01 to tar file using the following command:
    # docker save -o backup01.tar backup01
    # ls -al | grep back
    -rw------- 1 root root 178697728 Mar 31 23:35 backup01.tar

    You may choose to save the tar file on NFS mount point. Another option is directly pushing the image backup01 to your local registry. Before pushing the backup image, we need to tag it appropriately.

    # docker tag backup01 localhost:5000/backup-image:v1

    In this example, localhost is the hostname where the local registry is located and 5000 is the port number that the registry listens on. If you are working on a Docker engine located on a different host to the registry, you must change the hostname to point to the correct host. Note the repository and tag name, backup-image:v1 in the example, must all be in lower case to be a valid tag.

    # docker push backup-image:v1

    Restoring a Docker Container

  2. Image can be extracted from backup tar file using the following command
    # docker load -i /tmp/backup01.tar
    ff91b8b5abb1: Loading layer [==================================================>] 2.56 kB/2.56 kB
    Loaded image: backup01:latest

    You can create container from this image using “docker run“. If you had data volume on the original container. You must restore the data volume too and run container with the data volume (docker run -v)

  3. In-case of pushed image. You can directly pull it.
    # docker pull localhost:5000/backup-image:v1

    Tips :

Batch backup docker images

for image in `docker images | grep -v REPOSITORY | awk '{print $1":"$2}'`; do
    echo "saving the image of ${image}"
    docker save ${image} >  ${image////-}.tar
    echo -e "finished saving the image of �33[32m ${image} �33[0m"
done

Batch  restore the backup docker images

for image in `ls *.tar`; do
    echo "loading the image of ${image}"
    docker load < ${image}
    echo -e "finished loading the image of �33[32m ${image} �33[0m"
done
Source https://www.thegeekdiary.com/how-to-backup-and-restore-docker-containers/ https://neatlifecoco.com/tools.html
Comments