How to copy Docker image from one machine to another?
Contents
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.
1 2 3 4 5 |
docker images REPOSITORY TAG IMAGE ID CREATED SIZE artifactory.rc.com/repo/test_app_snapshots app_build_001 65a87f3cfe0e 24 hours ago 855MB artifactory.rc.com/repo/test_app_snapshots app_build_002 fb9f58ef7877 24 hours ago 852MB |
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.
1 |
docker save -o <generated tar file name> <repository_name:tag_name> |
Example :
1 |
docker save -o app_build_001.tar artifactory.rc.com/repo/test_app_snapshots:app_build_001 |
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.
1 |
scp app_build_001.tar remote_username@172.10.10.2:/remote/directory |
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
1 |
docker load -i <path to image tar file> |
Example
1 |
docker load -i app_build_001.tar |
The docker image app_build_001 is successfully loaded in the respective machine. Now it is ready to deploy our application in docker container.
1 2 3 4 |
docker images REPOSITORY TAG IMAGE ID CREATED SIZE artifactory.rc.com/repo/test_app_snapshots app_build_001 65a87f3cfe0e 24 hours ago 855MB |