# Prefer Docker save and load

over Docker export and import.

This serves as a reminder for me as I often mixed up myself.

## Save vs Export

As per documentation, [save](https://docs.docker.com/engine/reference/commandline/save/) produces a tar file that contains

> all parent layers, and all tags + versions, or specified repo:tag, for each argument provided

```bash
docker save busybox:latest > busybox.tar
```

while [export](https://docs.docker.com/engine/reference/commandline/export/) also produces a tar file, it only exports the content without layer/history

> export the contents of the *underlying* directory

```bash
docker export busybox:latest > busybox.tar
```

What this also means is that using the `export` will result in a smaller size image due to flattening.

## Load vs Import

[load](https://docs.docker.com/engine/reference/commandline/load/) restores the full history and layers from what was `saved`

> Load an image or repository from a tar archive (even if compressed with gzip, bzip2, or xz) from a file or STDIN. It restores both images and tags.

```bash
docker load < busybox.tar
```

while [import](https://docs.docker.com/engine/reference/commandline/import/) just creates the filesystem with the `exported content`

> Import the contents from a tarball to create a filesystem image

```bash
docker import busybox.tar
```

## When to use one over another?

In most cases, it wouldn't matter much.

However, from the perspective of running the command on an internet-enabled machine vs an air-gapped machine, there is a slight difference and you should really prefer to use `save and load` combination.

Why? Because on an air-gapped machine, you might the full history/layers to load successfully, otherwise, you may encounter errors where there are missing layers/images. From my very very vague memory, the digest seems to be the same whether you run `load or import`.

References:

* [https://stackoverflow.com/questions/36925261/what-is-the-difference-between-import-and-load-in-docker](https://stackoverflow.com/questions/36925261/what-is-the-difference-between-import-and-load-in-docker)
    
* [https://stackoverflow.com/questions/22713551/how-to-flatten-a-docker-image](https://stackoverflow.com/questions/22713551/how-to-flatten-a-docker-image)
    
* [https://tuhrig.de/flatten-a-docker-container-or-image/](https://tuhrig.de/flatten-a-docker-container-or-image/)
