Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between save and export in Docker?

Tags:

docker

People also ask

What does docker export do?

Docker export is a command that is used to export the container's file system as an archive i.e. tar that can be imported when required as a Docker image using the Docker import command.

When should you use docker export?

You can use the docker export command to export a container to another system as an image tar file. You need to export separately any data volumes that the container uses.

What should I save a Dockerfile as?

The docker export - Export a container's filesystem as a tar archive. The docker import - Import the contents from a tarball to create a filesystem image. The docker save - Save one or more images to a tar archive (streamed to STDOUT by default)

How do I save a docker container?

The usual way is at least through a docker commit: that will freeze the state of your container into a new image. But know that there is no reliable way to "save state" of container unlike virtual machine save state in Hyper-V or VMware. This is a downside also to 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.

  1. save command saves whole image with history and metadata but export command exports only files structure (without history and metadata). So the exported tar file will be smaller then the saved one.

  2. 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.


Technically, save/load works with repositories which can be one or more of images, also referred to as layers. An image is a single layer within a repo. Finally, a container is an instantiated image (running or not).


Docker save Produces a tar file repo which contains all parent layers, and all tags + versions, or specified repo:tag, for each argument provided from image.

Docker export Produces specified file(can be tar or tgz) with flat contents without contents of specified volumes from Container.

docker save need to use on docker image while docker export need to use on container(just like running image)

Save Usage

docker save [OPTIONS] IMAGE [IMAGE...]

Save an image(s) to a tar archive (streamed to STDOUT by default)

--help=false Print usage -o, --output="" Write to a file, instead of STDOUT

export Usage

docker export [OPTIONS] CONTAINER

Export the contents of a container's filesystem as a tar archive

--help=false Print usage -o, --output="" Write to a file, instead of STDOUT