SoFunction
Updated on 2025-03-10

Docker implements code examples for importing and exporting image (Image) files

Preface

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish them to any popular Linux machine, and can also be virtualized. In Docker, images are the basis of containers, including all file systems and configurations required for applications to run. Sometimes we need to export the Docker image to a file for use elsewhere, or deploy without a network connection. The following will describe in detail how to import and export image files using Docker.

1. Export Docker image

To export a Docker image, you can usedocker saveOrder. This command packages the specified image into a tar archive file and saves it in the local file system.

Command format

docker save -o <Output file name>.tar <Mirror name>:<Label>

Or, if not used-oOptions, you can also redirect output to a file, such as:

docker save <Mirror name>:<Label> > <Output file name>.tar

Example

Assume that you want to export the nameubuntu, labeledlatestand save it asubuntu_latest.tarTo file, you can use the following command:

docker save -o ubuntu_latest.tar ubuntu:latest

or

docker save ubuntu:latest > ubuntu_latest.tar

2. Import Docker image

To import a Docker image, you can usedocker loadOrder. This command loads the image from the specified tar archive into the local image library.

Command format

docker load -i <Enter a file name>.tar

Alternatively, if you do not use the -i option, you can also pipe the file contents to the docker load command, but this method is less common, and it is usually more intuitive to use the -i option directly.

Example

Assume that you want to import the saved ones beforeubuntu_latest.tarTo file, you can use the following command:

docker load -i ubuntu_latest.tar

3. Things to note

  • usedocker saveThe exported tar file contains the complete historical information of the mirror, anddocker exportThe file exported by the command is lightweight and does not contain historical information. Usually, we usedocker saveTo export and import images.
  • Export and import operations do not change the image's ID and creation time, and this information is retained in the imported image.
  • If the image is imported, the image with the same name is already locally available.docker loadAn error may be reported. So, before importing, you can usedocker rmiCommand deletes local image with the same name, or use-fForced coverage.

This is the article about the code example of Docker import and export image (Image) files. For more related Docker import and export Image content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!