Docker: How to save images & containers in a tar file and restore them on different machine ?

I. Playing with Docker Images

1. Take backup of an image

$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
mariadb                 10.1                5a34bfc8f676        2 weeks ago         375MB
phpmyadmin/phpmyadmin   latest              15ca549393be        5 weeks ago         166MB
php                     7.2.1-apache        f99d319c7004        12 months ago       377MB

### Save the image
$ docker save 15ca549393be > /tmp/phpmyadmin_image.tar

### Checkout the generated tar file
$ file /tmp/phpmyadmin_image.tar
/tmp/phpmyadmin_image.tar: POSIX tar archive
$ ls -l /tmp/phpmyadmin_image.tar
-rw-r--r--  1 saket1447583  wheel  174096384 Jan 15 07:03 /tmp/phpmyadmin_image.tar
$ du -sh /tmp/phpmyadmin_image.tar
166M	/tmp/phpmyadmin_image.tar
$ 

2. Remove image

$ docker rmi 5a34bfc8f676 15ca549393be f99d319c7004
Error response from daemon: conflict: unable to delete 5a34bfc8f676 (must be forced) - image is being used by stopped container 18d933aeca50
Error response from daemon: conflict: unable to delete 15ca549393be (must be forced) - image is being used by stopped container 82181b391f92
Error response from daemon: conflict: unable to delete f99d319c7004 (must be forced) - image is being used by stopped container 265e1f9c08a0

### Remove them forcefully
$ docker rmi 5a34bfc8f676 15ca549393be f99d319c7004 -f
Untagged: mariadb:10.1
Untagged: mariadb@sha256:9a67b8153e9c7a7879011449d44e33a7a1c74c8fa8fd29484c6214c9929eb55e
Deleted: sha256:5a34bfc8f676e93f8607e2d17658953655cb4ff7d00ac61b346ab6d29e641545
Untagged: phpmyadmin/phpmyadmin:latest
Untagged: phpmyadmin/phpmyadmin@sha256:8811552a9939b920bb436c023d3df17e4d9bdec60eeb74eb84a21ecb7e7ab0a7
Deleted: sha256:15ca549393be8da6be36ef39a4b694226059c3b9d4481df1854f153218767977
Untagged: php:7.2.1-apache
Untagged: php@sha256:3753d298b1af5b3975518175f29b611edf3b15b27204c917e8283b27df01c619
Deleted: sha256:f99d319c700441fbca80b85f96e8d6c63061d8c50e8c61e7138d101bcd712ab1
$ 

$ docker images ls --all
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
$ 

3. Restore image on same/different system

$ docker image load < phpmyadmin_image.tar
Loaded image ID: sha256:15ca549393be8da6be36ef39a4b694226059c3b9d4481df1854f153218767977
$ docker images --all
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
                            15ca549393be        5 weeks ago         166MB
$ 


Tip: Tagging an image

$ docker images --all
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
                            15ca549393be        5 weeks ago         166MB
$ 

$ docker image tag 15ca549393be phpmyadmin:1.7 
$ docker images --all
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
phpmyadmin          1.7                 15ca549393be        5 weeks ago         166MB


II. Playing with Docker Containers

Containers can also be saved and restored similar to images.
Just have a quick view how to do this.

### Save the container to tar file
$ docker export 265e1f9c08a0 > /tmp/container_php.tar
$ file /tmp/container_php.tar
/tmp/container_php.tar: POSIX tar archive
$ du -sh /tmp/container_php.tar
361M	/tmp/container_php.tar



### Remove any existing containers & images 
### and restore the saved container as image
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
$ docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ docker import /tmp/container_php.tar 
sha256:5622104ad36d16864149d7421bfdf185bba11bfc1776a976687a4ac20fe6da9a
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
                            5622104ad36d        37 seconds ago      370M



It is recommended to commit the docker container in order to save the container changes into the image and then we can save/load it later.

Leave a Reply

Your email address will not be published.