What is the difference between save and export in Docker?
The short answer is:
save
will fetch an image : for a VM or a physical server, that would be the installation .ISO image or disk. The base operating system.It will pack the layers and metadata of all the chain required to build the image. You can then load this "saved" images chain into another docker instance and create containers from these images.
export
will fetch the whole container : like a snapshot of a regular VM. Saves the OS of course, but also any change you made, any data file written during the container life. This one is more like a traditional backup.It will give you a flat .tar archive containing the filesystem of your container.
Edit: as my explanation may still lead to confusion, I think that it is important to understand that one of these commands works with containers, while the other works with images.
An image has to be considered as 'dead' or immutable, starting 0 or 1000 containers from it won't alter a single byte. That's why I made a comparison with a system install ISO earlier. It's maybe even closer to a live-CD.
A container "boots" the image and adds an additional layer on top of it. This layer stores any change on the container (created/changed/removed files...).
There are two main differences between save
and export
commands.
save
command saves whole image with history and metadata butexport
command exports only files structure (without history and metadata). So the exported tar file will be smaller then the saved one.When you use exported file system for creating a new image then this new image will not contain any
USER
,EXPOSE
,RUN
etc. commands from your Dockerfile. Only file structure will be transferred. So when you are using mentioned keywords in your Dockerfile then you cannot use export command for transferring image to another machine - you need always use save command.
The exported image will not have any layer or history information saved, so it will be smaller and you will not be able to rollback.
The saved image will have layer and history information, so larger.
If giving this to a customer, the Q is do you want to keep those layers or not?
export
: container (filesystem)
->image tar
.import
: exported image tar
-> image
. Only one layer.
save
: image
-> image tar
.load
: saved image tar
->image
. All layers will be recovered.
From Docker in Action, Second Edition p190.
Layered images maintain the history of the image, container-creation metadata, and old files that might have been deleted or overridden.
Flattened images contain only the current set of files on the filesystem.