-pull docker image
# docker pull busybox
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 8ac48589692a 12 days ago 1.15MB
-make changes to a container
# docker run busybox mkdir /home/test
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87fa2f411346 busybox “mkdir /home/test” About a minute ago Exited (0) About a minute ago festive_spence
-commit this changed container and create a new image called busybox-1
# docker commit 87fa2f411346 busybox-1
sha256:4ac0618d96c9f1a5cc6fef88e8310bdd32f4af7da84bf77f712c36e439b2abae
-should see the image busybox and busybox-1
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox-1 latest 4ac0618d96c9 16 seconds ago 1.15MB
busybox latest 8ac48589692a 12 days ago 1.15MB
-to see the difference between both images
# docker run busybox [ -d /home/test ] && echo ‘Directory found’ || echo ‘Directory not found’
Directory not found
# docker run busybox-1 [ -d /home/test ] && echo ‘Directory found’ || echo ‘Directory not found’
Directory found
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66db57b1054b busybox-1 “[ -d /home/test ]” 4 minutes ago Exited (0) 4 minutes ago suspicious_pike
2f6107852442 busybox “[ -d /home/test ]” 4 minutes ago Exited (1) 4 minutes ago infallible_euclid
-Export is used to persist a container (not an image)
NOTE: you can export while container running
# docker export 2f6107852442 > /tmp/export.tar
The result is a TAR-file which should be slightly smaller than the one from save
-Save is used to persist an image (not a container)
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox-1 latest 4ac0618d96c9 8 minutes ago 1.15MB
busybox latest 8ac48589692a 12 days ago 1.15MB
# docker save busybox-1 > /tmp/save.tar
The result is a TAR-file which should be slightly bigger than the one from export
-we clean up a little bit – we remove all containers and images we have right now
# docker rmi busybox busybox-1
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
-restore an export.tar
# cat /tmp/export.tar | sudo docker import – busybox-1-export:latest
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox-1-export latest 4ca587fe5d23 13 seconds ago 1.15MB
# sudo docker run busybox-1-export [ -d /home/test ] && echo ‘Directory found’ || echo ‘Directory not found’
Directory not found
-restore a save.tar
# docker load < /tmp/save.tar
0314be9edf00: Loading layer 1.36MB/1.36MB
7b125480d284: Loading layer 2.048kB/2.048kB
Loaded image: busybox-1:latest
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox-1-export latest 4ca587fe5d23 5 minutes ago 1.15MB
busybox-1 latest 4ac0618d96c9 About an hour ago 1.15MB
# docker run busybox-1 [ -d /home/test ] && echo ‘Directory found’ || echo ‘Directory not found’
Directory found
So what’s the difference between both?
Well, as we saw the exported version is slightly smaller. That is because it is flattened, which means it lost its history and meta-data.
We can see this by the following command:
# docker history busybox-1-export
IMAGE CREATED CREATED BY SIZE COMMENT
4ca587fe5d23 8 minutes ago 1.15MB Imported from –
# docker history busybox-1
IMAGE CREATED CREATED BY SIZE COMMENT
4ac0618d96c9 About an hour ago mkdir /home/test 0B
<missing> 12 days ago /bin/sh -c #(nop) CMD [“sh”] 0B
<missing> 12 days ago /bin/sh -c #(nop) ADD file:c94ab8f861446c74e… 1.15MB