How to copy Docker images from one host to another without using a repository?

DockerOperating SystemOpen Source

If you have a Docker image in your own local machine and you want that image to be copied into another machine, there are two ways to do that. The first is by pushing that image to a repository such as the ones in Dockerhub registry. You need to have an account in Dockerhub and then you can use the Docker push command to push the images on it.

However, if you don’t want to go through all the hassles of creating an account, tagging the images, etc., there are other simple methods that you can use. Let’s check out all such methods in this article.

Method 1. Saving and Loading the Image from TAR files

Docker allows you to save images into tar files using the Docker save command. This will also compress the entire image and will allow you to share them easily and quickly. You can then use the Docker load command in another machine to load the Docker image back from the tar file. The commands to do so are -

$ docker save -o <tar file path in source host machine> <image name>

You can then copy this tar file using simple tools such as cp, rsync, scp, etc. or any other method that you prefer. Next, you can use the Docker load command to restore the image from this tar file.

$ docker load -i <path to image tar file>

Method 2. Copy Docker Images Via SSH

You can also transfer your Docker images through SSH and bzip the content to compress it on the fly. The command to do so is -

$ docker save <image> | bzip2 | \
   ssh user@host 'bunzip2 | docker load'

If you want to check how the transfer is taking place, you can use the pv through the pipe.

$ docker save <image> | bzip2 | pv | \
   ssh user@host 'bunzip2 | docker load'

Method 3. Copying Docker Images using Docker Machines

If you have two Docker machines - machine1 and machine2, you can copy the images using the following command.

$ docker $(docker-machine config machine1) save <image> | docker $(docker-machine config machine2) load

Method 4. Copying Images Using DOCKER_HOST variable

You can also use the DOCKER_HOST variable to copy images from one host to another. You will need the SSH credentials and both the users on the local and remote machines should be in the Docker group.

$ docker save <image name>:<tag-name> | gzip | DOCKER_HOST=ssh://user@remotehost docker load

Method 5. Use Docker-push-ssh to copy images

You have another command-line utility called docker-push-ssh. It will help you to set up a private Docker registry which is temporary on the host server. It will then create an SSH tunnel from the localhost. Next, it will push the Docker image and automatically clean up.

The advantage of using this method instead of the docker save command is that in this case only the new layers are always pushed to the server. This results in a faster upload.

To do so, you will have to first install docker-push-ssh using the following pip command.

$ pip install docker-push-ssh

You can then use the one-line to push the image.

$ docker-push-ssh -i ~/your-ssh-key <docker-image>

Method 6. Transferring Image using Docker Machine Scp

The Docker Machine scp was created to copy or transfer files from a particular Docker machine to another. It becomes extremely convenient in case you want to copy images from the local machine to a remote Docker machine on cloud such as AWS or Digital Ocean. This is so because the Docker Machine will automatically take care of the SSH credentials.

To do so, you can follow these steps -

  • Use Docker save command.
$ docker save -o <tar-file-location> <image name>
  • Transfer Docker Images using docker machine scp.
$ docker-machine scp ./image-tar.tar target-machine:/home/ubuntu

Here, target-machine is the name of the remote machine, /home/ubuntu is the target location, and image-tar is the name of the tar file.

  • Load the tar file.
$ docker-machine ssh target-machine sudo docker load -i image-tar.tar

To sum up, in this article, we discussed 6 different methods using which you can copy or transfer Docker images from one local machine to another remote machine without the use of any Docker registry or repository. As per your convenience and requirements, you can easily use any of the discussed methods.

Published on 06-Aug-2021 11:46:48