How to copy Docker image from one machine to another?

Docker image

A Docker image is an executable package that includes everything needed to run a container: the code, a runtime, libraries, environment variables, and configuration files. The docker images can be stored in public or private repository. Docker Hub is a public repository where we can share the image to Docker community. But the companies have private repository to store their docker images.

Lets consider that we have two servers to run our docker images. One server have access to pull the docker image from the private repository. On the other hand, another server doesn’t have access to pull the docker image. But using docker commands, we can copy the docker image from one machine to another. In this article, we are explaining the steps to copy the docker image from one server to another server.

Step 1 : Get the docker repository and tag name in machine 1

The command docker images will show all the images details, their repository, tags, and size. As we can see below, it is showing the repository and tag name for each docker image. In this example, we are going to copy the docker image with the tag name as app_build_001.

Step 2 : Save the Docker image as a tar file in machine 1

The command docker save is used to save the docker image as tar file. As mentioned below, we need to mention the repository name and tag name of the docker image. Also we need to give the output tar file name.

Example :

The tar file app_build_001.tar is created successfully in the directory where we executed this command.

Step 3 : Copy the tar file to machine 2

Now we can use cp, scp or rsync command to copy the tar file to another machine. In this example, we are showing the scp command for reference.

Step 4 : Load the image into Docker (in the machine 2)

Once we copy the tar file to respective machine, we need to run docker load command to load the docker image from tar file. Lets execute this command for the tar file app_build_001.tar

Example

The docker image app_build_001 is successfully loaded in the respective machine. Now it is ready to deploy our application in docker container.