Import from rootfs archive

Format: docker import [options] <file>|<URL>|- [<repository name>[:<label>]]

The tarball can be a local file, a remote web file, or even from standard input. The tarball will be expanded in the mirror / directory and submitted directly as the first layer of the mirror.

Let’s say we want to create a mirror of an Ubuntu 16.04 template  for OpenVZ:

$ docker import \
    http://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz \
    openvz/ubuntu:16.04

Downloading from http://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz
sha256:412b8fc3e3f786dca0197834a698932b9c51b69bd8cf49e100c35d38c9879213 

This command automatically downloads the ubuntu-16.04-x86_64.tar.gz file, expands it as the root filesystem, and saves it as the image openvz/ubuntu:16.04.

After the import is successful, we can see the imported image using docker image ls:

$ docker image ls openvz/ubuntu
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
openvz/ubuntu       16.04               412b8fc3e3f7        55 seconds ago      505MB 

If we look at its history, we’ll see that there are links to the imported files in the description:

$ docker history openvz/ubuntu:16.04
IMAGE               CREATED              CREATED BY          SIZE                COMMENT
f477a6e18e98        About a minute ago                       214.9 MB            Imported from http://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz 

Importing and Exporting Docker Images – docker save and docker load

Docker also provides the docker save and docker load commands for saving an image as a file, transferring it to another location, and then loading it in. This was the practice when there was no Docker Registry, but it is no longer recommended and image migrations should be done directly using the Docker Registry, either directly from the Docker Hub or from a private intranet registry.

Saving Images

Use the docker save command to save an image as an archive.

For example, we want to save this alpine image.

$ docker image ls alpine
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              baa5d63471ea        5 weeks ago         4.803 MB 
$ docker save alpine -o filename
$ file filename
filename: POSIX tar archive 

The filename here can be any name or even any extension, but the file is an archive by nature.

Note: If the same name is used, it will be overwritten (without warning).

If you use gzip compression:

$ docker save alpine | gzip > alpine-latest.tar.gz 

Then we copied the alpine-latest.tar.gz file to another machine where we can load the image with the following command:

$ docker load -i alpine-latest.tar.gz
Loaded image: alpine:latest 

If we combine these two commands with ssh and pv, using Linux’s powerful pipelines, we can write a command to migrate an image from one machine to another with a progress bar:

docker save <image name> | bzip2 | pv | ssh <username>@<hostname> ‘cat | docker load’ 

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

Catalogue