导入和加载在 Docker 中的区别是什么?

我理解 export(对于容器)和 save(对于图像)之间的区别。但是在一天结束时,通过保存或导出生成的 tarball 应该被用作 一幅画面

那么,为什么有两个命令从一个 tarball 创建一个图像呢?

67743 次浏览

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

  • 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

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.

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.

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.