What is the difference between import and load in Docker?
Asked Answered
N

4

109

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?

Neau answered 28/4, 2016 at 20:53 Comment(0)
I
146

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.

Impersonate answered 29/4, 2016 at 7:47 Comment(5)
Is docker image import a synonym for docker import?Tansey
Probably (I am on my phone), considering the syntax docker image only came later.Impersonate
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.Imprudent
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 imageAnchoress
@NadirLaskar No: that is requested in github.com/moby/moby/issues/8039. There is a workaround: github.com/moby/moby/issues/8039#issuecomment-480121360, but it might not apply to your specific use-case though.Impersonate
A
33

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.

Anaesthetize answered 23/7, 2019 at 12:22 Comment(0)
S
3

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.

Slobber answered 22/4, 2019 at 21:15 Comment(0)
A
3

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.

Aver answered 23/5, 2022 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.