Docker - backup and restore container

Posted on Wed 19 October 2016 by Pavlo Khmel

Example below shows how to backup on server A and restore docker containers on server B.

As example I'll use container created in the previous post Docker – wordpress all-in-one container on CentOS 7

Note: this example stores all data inside writable container layer. All data will be lost when you delete container. Recommended way is to store data on persistent volumes. So you do not need to backup container. You only need to backup volumes.

0. Stop container

As I used systemd enabled container I need to use shutdown command instead of docker stop

docker exec -ti wordpress /usr/sbin/shutdown -h now

1. Commit changes to the backup images

docker commit wordpress local/wordpress-20161017

List all images:

docker images
REPOSITORY                  TAG    IMAGE ID     CREATED        SIZE
local/wordpress-20161017    latest 0c36e3eaca81 11 seconds ago 678.5 MB
local/c7-wordpress-20161017 latest 6cde1b8a8ed9 8 hours ago    678.8 MB
local/wordpress             latest 05a8b6d85d8d 9 hours ago    645.8 MB

2. Save container to the tar file

docker save local/wordpress-20161017 > /root/docker-wordpress-20161017.tar

3. Restore image on server B

scp /root/docker-wordpress-20161017.tar 192.168.0.2:/root/
ssh 192.168.0.2
docker load -i docker-wordpress-20161017.tar 
docker images
REPOSITORY               TAG    IMAGE ID     CREATED       SIZE
local/wordpress-20161017 latest 0c36e3eaca81 6 minutes ago 678.5 MB

4. Run backup container

docker run --restart=always --privileged --name wordpress -td -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 local/wordpress-20161017

Check:

docker ps -a
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                     PORTS                NAMES
b45255b078c7        local/wordpress-20161017   "/usr/sbin/init"    9 seconds ago       Up 7 seconds               0.0.0.0:80->80/tcp   wordpress