How to transfer images & containers

Content

It is possible that you want to save an image or a container and transfer it between computers, without using the Docker Hub functionality. For this reason Docker provides the appropriate commands.
There is a distinction between the two modes. When you are saving an image, it is usually to move a stable environment you have already created, while when you are exporting a container you get the base image the container is using + any changes you have made inside the running container. A new Image is going to be created in both cases in the receiving host.

Transferring an image

In case you want to move a stable environment to be used again as a base image in another computer.

Save

By default the output of save is streamed to STDOUT, so you have to redirect to a file.

The filename provided must end in “.tar” or “tar.gz”.

# Command format
docker save my_image:image_tag > my_image-image_tag.tar        # e.g. docker save alpine > alpine.tar
docker save --output my_image-image_tag.tar my_image:image_tag # e.g. docker save --output alpine.tar alpine
docker save -o my_image-image_tag.tar my_image:image_tag       # e.g. docker save -o alpine-latest.tar alpine:latest
 
# Also compress to reduce the file size.
docker save myimage:latest | gzip > myimage_latest.tar.gz      # e.g. docker save alpine:latest | gzip > alpine_latest.tar.gz

The image_tag is optional.

Load

The load command will create a new image from the file you specify.

# Command format
docker load < my_image.tar.gz    # e.g. docker load < alpine.tar.gz
docker load --input my_image.tar # e.g. docker load --input alpine.tar

The image_tag is optional.

Transferring a container

In case want to export changes made inside a running container + the image, so you can continue working on the changes.

Export

The export command will include the changes made inside the container in the file that will be created.

The command does not export the contents of volumes associated with the container.

# Command format
docker export my_container_name > exported_container.tar          # docker export boring_wozniak > exported_container.tar
docker export --output="exported_container.tar" my_container_name # docker export --output="exported_container.tar" boring_wozniak

The image_tag is optional.

To get the container name (my_container_name) use docker ps -a command and find it at the last column. screenshot

Import

The import command will create a new image from the file specified, which originated from a container and thus includes any changes occurred. Again, the import reads by default from STDOUT so we redirect the output of cat into the command.

# Command format
cat exported_container.tgz | docker import - my_image:image_tag # e.g. cat exported_container.tgz | docker import - modified_alpine:latest

The image_tag is optional.

See also