Search code examples
dockerdocker-image

What is the difference between import and load in Docker?


I understand the difference between export (for containers) and save (for images). But at the end of the day the tarball produced by either save or export should be used as an image.

So why are there 2 commands to make an image from a tarball?


Solution

  • docker save will indeed produce a tarball, but with all parent layers, and all tags + versions.

    docker export does also produce a tarball, but without any layer/history.

    It is often used when one wants to "flatten" an image, as illustrated in "Flatten a Docker container or image" from Thomas Uhrig:

    docker export <CONTAINER ID> | docker import - some-image-name:latest
    

    However, once those tarballs are produced, load/import are there to:

    • docker import creates one image from one tarball which is not even an image (just a filesystem you want to import as an image)

      Create an empty filesystem image and import the contents of the tarball

      By itself, this imported image will not be able to be run from docker run, since it has no metadata associated with it (e.g. what the CMD to run is.)

    • docker load creates potentially multiple images from a tarred repository (since docker save can save multiple images in a tarball).

      Loads a tarred repository from a file or the standard input stream

      By using load you can import the image(s) in the same way they were originally created with the metadata from the Dockerfile, so you can directly run them with docker run.