I recently played around with Docker, an application container and virtualization technology for Linux. It was pretty cool and I was able to create Docker images and containers within a couple of minutes. Everything was working right out of the box!
At the end of my day I wanted to persist my work. I stumbled over the Docker commands save
and export
and wondered what their difference is. So I went to StackOverflow and asked a question which was nicely answered by mbarthelemy. Here is what I found out.
How Docker works (in a nutshell)
Docker is based on so called images. These images are comparable to virtual machine images and contain files, configurations and installed programs. And just like virtual machine images you can start instances of them. A running instance of an image is called container. You can make changes to a container (e.g. delete a file), but these changes will not affect the image. However, you can create a new image from a running container (and all it changes) using docker commit <container-id> <image-name>
.
Let’s make an example:
Now we have two different images (busybox
and busybox-1
) and we have a container made from busybox
which also contains the change (the new folder /home/test
). Let’s see how we can persist our changes.
Export
Export is used to persist a container (not an image). So we need the container id which we can see like this:
To export a container we simply do:
The result is a TAR-file which should be around 2.7 MB big (slightly smaller than the one from save
).
Save
Save is used to persist an image (not a container). So we need the image name which we can see like this:
To save an image we simply do:
The result is a TAR-file which should be around 2.8 MB big (slightly bigger than the one from export
).
The difference
Now after we created our TAR-files, let’s see what we have. First of all we clean up a little bit – we remove all containers and images we have right now:
We start with our export we did from the container. We can import it like this:
We can do the same for the saved image:
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:
If we run the command we will see an output like the following. As you can see there, the exported-imported image has lost all of its history whereas the saved-loaded image still have its history and layers. This means that you cannot do any rollback to a previous layer if you export-import it while you can still do this if you save-load the whole (complete) image (you can go back to a previous layer by using docker tag <LAYER ID> <IMAGE NAME>
).
Best regards,
Thomas