Script to clone / snapshot Docker containers, including their data?

I would like to clone an attached application, including all its data, which uses three containers in this example: 1) a web application container such as CMS, 2) a database container and 3) a data volume container (using dockers).

With docker layout, I can easily create identical instances of these containers with only the source data. But what if I want to clone a set of running containers on a single server, including all its accumulated data, similar to how I would clone a KVM container? With KVM, I would pause or shut down the virtual machine, clone something like virt-clone, and then start a cloned guest that has all the same data as the original.

One use case would be to create a clone / snapshot of a working development web server before making major changes or before installing new versions of plugins.

With Docker, this does not seem so simple, because data is not automatically copied along with the container. Ideally, I would like to do something simple, for example docker-compose clone, and get a second set of containers that are identical to the first, including all their data. Neither Docker nor docker-compose provide the clone command (since version 1.8), so I will need to consider various approaches, such as backing up and restoring data / databases, or using a third-party tool like Flocker.

In this regard, the question arises of how to make something similar to the KVM snapshots of a docked application, with the ability to easily return to the previous state. Preferably, cloning, snapshots, and reversing should be possible with minimal downtime.

What would be Docker's preferred way to accomplish these tasks?

Change Based on the first answer, I will make my question a little more specific, so I hope to come to the program steps so that I can do something like docker-compose-cloneand docker-compose-snapshotusing a bash or python script. Cloning the contents of docker volumes becomes the key for him, since the containers themselves are basically cloned every time I run docker-compose in the same yaml file.

Generally, my full-clone script should

  • , docker
  • ( )
  • ,
    • ? , 4 ( ) script, docker volume ls Docker 1.9.

    • - KVM, ? (, COW ZFS, Docker ).

+4
3

. , ( , ).

, . , docker 1.9 apis , , .

-, .

0

script, - CMS Concrete5.7 . , .

script:

#!/bin/bash
set -e

# This script will clone a set of containers including all its data

# the docker-compose.yml is in the PROJ_ORIG directory
# - do not use capital letters or underscores for clone suffix, 
#   as docker-compose will modify or remove these
PROJ_ORIG="c5app"
PROJ_CLONE="${PROJ_ORIG}003"

# 1. duplicate the directory containing the docker-compose file
cd /opt/docker/compose/concrete5.7/
cp -Rv ${PROJ_ORIG}/ ${PROJ_CLONE}/

# 2. temporarily stop the containers
cd ${PROJ_ORIG}
docker-compose stop

# 3. create, run and stop the second set of containers 
#    (docker-compose does not have a create command)
cd ../${PROJ_CLONE}
docker-compose up -d
docker-compose stop

# 4. determine the data-volumes to be duplicated
#   a) examine which containers are designated data containers
#   b) then use docker inspect to determine the relevant directories
#   c) store destination directories & process them for backup and clone
#
# In this appliaction we use two data containers 
# (here we used DATA as part of the name):
# $ docker-compose ps | grep DATA
#     c5app_DB-DATA_1    /true                            Exit 0
#     c5app_WEB-DATA_1   /true                            Exit 0
#
# $ docker inspect ${PROJ_ORIG}_WEB-DATA_1 | grep Destination
#     "Destination": "/var/www/html",
#     "Destination": "/etc/apache2",
#
# $ docker inspect ${PROJ_ORIG}_DB-DATA_1 | grep Destination
#     "Destination": "/var/lib/mysql",

# these still need to be determined manually from examining 
# the docker-compose.yml or using the commands in 4.
DATA_SUF1="_WEB-DATA_1"
VOL1_1="/etc/apache2"
VOL1_2="/var/www/html"

DATA_SUF2="_DB-DATA_1"
VOL2_1="/var/lib/mysql"

# 5. Backup Data:
docker run --rm --volumes-from ${PROJ_ORIG}${DATA_SUF1} -v ${PWD}:/clone debian tar -cpzf /clone/clone${DATA_SUF1}.tar.gz ${VOL1_1} ${VOL1_2}
docker run --rm --volumes-from ${PROJ_ORIG}${DATA_SUF2} -v ${PWD}:/clone debian tar -cpzf /clone/clone${DATA_SUF2}.tar.gz ${VOL2_1}

# 6. Clone Data:
# existing files in volumes need to be deleted before restoring, 
# as the installation may have created additional files during initial run,
# which do not get overwritten during restore
docker run --rm --volumes-from ${PROJ_CLONE}${DATA_SUF1} -v ${PWD}:/clone debian bash -c "rm -rf ${VOL1_1}/* ${VOL1_2}/* && tar -xpf /clone/clone${DATA_SUF1}.tar.gz"
docker run --rm --volumes-from ${PROJ_CLONE}${DATA_SUF2} -v ${PWD}:/clone debian bash -c "rm -rf ${VOL2_1}/* && tar -xpf /clone/clone${DATA_SUF2}.tar.gz"

# 7. Start Cloned Containers:
docker-compose start

# 8. Remove tar archives
rm -v clone${DATA_SUF1}.tar.gz
rm -v clone${DATA_SUF2}.tar.gz

, :

  • , ,
  • script , .
  • snap-shot/restore

( 4.). , - , , .

, , docker-compose.yml .

0

Windows , Windocks, , . :

  • , . , , .
  • . , . . SQL Server.

    . .

0

Source: https://habr.com/ru/post/1609073/


All Articles