Wise people learn when they can; fools learn when they must - Arthur Wellesley

Sunday 3 April 2022

DOCKER-5

 

                DOCKER-5 (SAVE & RESTORE)   

What we will learn in this post,

·         HOW TO SAVE & RESTORE (IMAGE),

·         HOW TO SAVE & RESTORE (CONTAINER),

Let’s start…

SAVE & RESTORE (IMAGE),

[root@centos7-i2 ~]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

ubuntu       latest    ff0fea8310f3   2 weeks ago    72.8MB

nginx        latest    f2f70adc5d89   2 weeks ago    141MB

centos       latest    5d0da3dc9764   6 months ago   231MB

We have 3 images in local repo, now we will create backup of each.

[root@centos7-i2 ~]# docker save -o ubuntu_bkp.iso ubuntu

[root@centos7-i2 ~]# docker save -o nginx_bkp.iso nginx

[root@centos7-i2 ~]# docker save -o centos_bkp.iso centos

All backups are stored at current directory,

[root@centos7-i2 ~]# ls -lh *.iso

-rw-------. 1 root root 228M Apr  3 14:55 centos_bkp.iso

-rw-------. 1 root root 140M Apr  3 14:54 nginx_bkp.iso

-rw-------. 1 root root  72M Apr  3 14:54 ubuntu_bkp.iso

Now let’s remove image “nginx”

[root@centos7-i2 ~]# docker rmi nginx

Untagged: nginx:latest

Untagged: nginx@sha256:e1211ac17b29b585ed1aee166a17fad63d344bc973bc63849d74c6452d549b3e

Deleted: sha256:f2f70adc5d89aa922836e9cc6801980a12a7ff9012446cc6edf52ef8798a67bd

Deleted: sha256:67ebf5d071658c842ea9aa53588dea5bfb98635a2133151c0699c3eb487c114d

Deleted: sha256:1bbfd0f49839d5fd15e209e5b7a44bfa4347cfcdf7a288a8f1d39d5a56664418

Deleted: sha256:b689a5ad0c39e61cbd4532000b49f545169e27578b49492b5c3b368fe07d8337

Deleted: sha256:16343f21b7f9c1bcb2930c8b6b97b3f8df9136e58492421f0fda966c834e4746

Deleted: sha256:dfae993a20b32b42b6187b7e223c7f33cc19b2b092fe860a4c29f6dce551e91d

Deleted: sha256:3a626bb08c24b5cc968d312bf5694aa87b6d9961c5f182c6bc138d8ca8ac13ee

We have only 2 images left,

[root@centos7-i2 ~]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

ubuntu       latest    ff0fea8310f3   2 weeks ago    72.8MB

centos       latest    5d0da3dc9764   6 months ago   231MB

let’s restore the nginx image,

[root@centos7-i2 ~]# docker load -i nginx_bkp.iso

3a626bb08c24: Loading layer   83.9MB/83.9MB

30c00b5281a1: Loading layer     62MB/62MB

8b8ecda1d12d: Loading layer  3.072kB/3.072kB

2793e885dc34: Loading layer  4.096kB/4.096kB

d00147ef6763: Loading layer  3.584kB/3.584kB

24037b645d66: Loading layer  7.168kB/7.168kB

Loaded image: nginx:latest

[root@centos7-i2 ~]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

ubuntu       latest    ff0fea8310f3   2 weeks ago    72.8MB

nginx        latest    f2f70adc5d89   2 weeks ago    141MB

centos       latest    5d0da3dc9764   6 months ago   231MB

We can create containers from restored images.

SAVE & RESTORE (CONTAINER)

[root@centos7-i2 ~]# docker commit -p MYCENTOS_1 centos-MYCENTOS_1-backup

invalid reference format: repository name must be lowercase

[root@centos7-i2 ~]# docker commit -p MYCENTOS_1 centos-mycentos_1-backup

sha256:e32b8e6927d22fc4091038783e5cc43a1be0213ab331fe1767c654930873f8ca

[root@centos7-i2 ~]# docker save -o centos-mycentos_1-backup.tar centos-mycentos_1-backup

[root@centos7-i2 ~]# ls -ltrh *tar

-rw-------. 1 root root 228M Apr  3 16:14 centos-mycentos_1-backup.tar

What happened?

The container backup is created as Image; now load it to local image repo

**we can also push the image to docker hub or any other backup location

[root@centos7-i2 ~]# docker load -i centos-mycentos_1-backup.tar

Loaded image: centos-mycentos_1-backup:latest

[root@centos7-i2 ~]# docker images

REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE

centos-mycentos_1-backup   latest    e32b8e6927d2   5 minutes ago   231MB

ubuntu                     latest    ff0fea8310f3   2 weeks ago     72.8MB

nginx                      latest    f2f70adc5d89   2 weeks ago     141MB

centos                     latest    5d0da3dc9764   6 months ago    231MB

Now let’s check whether backup is working or not,

Stop & remove the container MYCENTOS_1

[root@centos7-i2 ~]# docker stop MYCENTOS_1

MYCENTOS_1

[root@centos7-i2 ~]# docker rm MYCENTOS_1

MYCENTOS_1

Now check for any running container,

[root@centos7-i2 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

No running container, let’s create the container from saved image,

[root@centos7-i2 ~]# docker run --name MYCENTOS_1 --restart always -it centos-mycentos_1-backup

[root@1182028c3421 /]# uptime

 10:49:50 up 35 min,  0 users,  load average: 0.06, 0.12, 0.14

[root@1182028c3421 /]#

[root@centos7-i2 ~]#

Well, we successfully restored the container.

2 comments:

  1. Are we going to get more lectures on docker or its sufficient.

    ReplyDelete
    Replies
    1. Definitely it is not sufficient, still the major parts like Networking and Volumes are pending. I will try to upload at earliest. time constrain is major issue with me, sorry for inconvenience.

      Delete