105

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?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
rdupz
  • 2,204
  • 2
  • 13
  • 21

4 Answers4

139

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.

slhck
  • 36,575
  • 28
  • 148
  • 201
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Is `docker image import` a synonym for `docker import`? – masterxilo Jun 08 '18 at 10:42
  • Probably (I am on my phone), considering the syntax docker image only came later. – VonC Jun 08 '18 at 11:24
  • 1
    Yes thats the exact difference. Just to add, as docker import or docker image import create a image from a filesystem, one can change / provide entrypoint, cmd, run etc. dockerfile instructions which option is not available with docker load or docker image load as this create 1/more images which already have it's layers & other history info preserved. – Saurabhcdt Mar 09 '19 at 13:04
  • Can we save docker images as tars without the dependent base layers? I have nodejs image based on nodejs image, when i save my image the size turns out to be 800mb because of the nodejs base image layer. Is there any way to export the images without the base image – Nadir Laskar Nov 11 '19 at 13:22
  • 1
    @NadirLaskar No: that is requested in https://github.com/moby/moby/issues/8039. There is a workaround: https://github.com/moby/moby/issues/8039#issuecomment-480121360, but it might not apply to your specific use-case though. – VonC Nov 11 '19 at 14:19
30

As a Docker-newbie, I learnt this difference the hard way.

  • On one system:

    docker run -it myImage /bin/bash
    

    --> Works fine

  • On that same system (using save):

    docker save myImage -o myImage.tar
    
  • On second system (using import):

    docker import myImage.tar
    

    --> Works nicely, no issues, just tag required:

    docker tag _the_assigned_tag myImage
    
  • On that second system:

    docker run -it myImage /bin/bash
    

    docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown.

Looking for that error brought my to all kinds of reasons such as MountFlags="slave", but the real reason turned out to be the one described in this post: I should have used load instead of import. Not knowing what was going on, Docker's error message didn't put me in any sense on track towards the "import" cause, till I stumbled over this post.

qootec
  • 409
  • 4
  • 3
2

docker import is mostly used with a tarball that is created out of running container. For Eg. docker export containerID > /home/cntr.tar then import this tarball to an image Eg. docker import /home/cntr.tar mynewimage:tag

Whereas docker load is used to load the image from a tarball that is created from another image. For Eg. docker save > /home/fromimg.tar then load it back with docker load < /home/fromimg.tar

the main difference though docker save/load with image does preserve the image history. Whereas docker export/import with container flattens the image by removing all the history of the container.

Shreedhar
  • 39
  • 4
2

I want to share another difference from real world situation using docker save and on prod servers using docker import vs docker load.

On the server with internet access docker import worked the same way as docker load. Container was up and running without error and missing layers have been downloaded over internet.

On the server without internet - on-prem setup docker import cause above error on container start e.g.

docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: "/bin/bash": stat /bin/bash: no such file or directory": unknown.

In order to get saved container up and running without that we need to use docker load < saved-container.tgz In that way all the layers are imported.

gsone
  • 1,188
  • 8
  • 25