Skip to content
Home ยป How to Back up and Restore Docker Image(s)?

How to Back up and Restore Docker Image(s)?

By default, if you want to create a Docker container on your server, you can download the required image directly using the docker pull command. But sometimes, there are some cases where you cannot download the image directly from the internet, and you have to back up the existing image and then restore the image to a server.

 

Problem

How to back up and restore Docker image(s)?

 

Solution

I have a server that, due to security issues, cannot be connected to the internet, while on the server, many applications run using Docker. Because it cannot directly download the Docker image, I have to download the Docker image on a server that is connected to the internet, and then the image will be installed on this server.

A. Backup Docker image

To back up a Docker image, use the format below:

docker image save -o image_name.tar image_name:tag

 

For example, if you want to back up the nginx image, then use the command below:

docker image save -o nginx.tar nginx

 

Wait until the process is complete, and if it is finished, the backup image file will be formed as shown below:

Back up one Docker image

 

But use the format below if you want to back up more than one image:

docker image save -o image1_name.tar image2_name:tag ...

 

I have more than one Docker image on my server, so I want to back up all the images so I can run the images on my other server, which is not connected to the internet. Then use the command below to back up the docker image of more than one Docker image:

docker image save -o all_images.tar alpine redis nginx

 

And the backup image file should be formed according to the image below:

Backup more than one docker image

 

B. Restore Image Docker

After you back up the Docker image, move your Docker image to the desired server. To restore the Docker image, use the format below:

docker image load -i image_name.tar

 

So I restored the Docker image backup file using the command below:

docker image load -i nginx.tar

 

Then the Nginx image will be restored on the server as shown below:

Restore one Docker image

 

With the same command, you can also restore more than one image using the command below:

docker image load -i all_images.tar

 

Then all Docker images will be restored on that server, like in the image below:

Restore more than one Docker image

 

Note

You can use the command below to back up the Docker image using the format below:

docker save image_name | gzip -c > image_name.tgz

 

So if you want to back up the Nginx image, use the command below:

docker save nginx | gzip -c > nginx.tgz

 

The advantage of using this command is that the size of the backup file is much smaller than using the previous command, as shown in the image below:

Back up the Docker image using another command

 

To restore, use the format below:

gunzip -c filename.tgz | docker load

 

So if you want to restore more than one image, use the command below:

gunzip -c all_images.tgz | docker load

 

And the Docker image will be restored on the server.

Restore the backup Docker image using another command

 

References

youtube.dimas-maryanto.com
youtube.com
docs.docker.com
stackoverflow.com

image_pdfimage_print
Visited 19 times, 1 visit(s) today
Tags:

Leave a Reply

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