SoFunction
Updated on 2025-04-11

Docker import and export images, containers, and solution to failure to start containers

Preface

With the development of container technology, many application systems now choose to use docker containers for deployment, but sometimes problems will be encountered when using docker containers for deployment. For example, our application needs to rely on other third-party images. If the server is unable to connect to the external network at this time, then it will not be deployed.

Based on this situation, docker officially supports the import and export of docker images and containers. We can compile the image on a network-connected machine, then export the image or container, and finally upload the exported image or container to the intranet server, and then import the image or container. This is all.

Import and export of mirrors

1.1 Docker image export

  • Order:
docker save [options] images [images……]
  • Use the command:
docker save -o /home/dockeruser/lgr/ Mirror name

or

docker save > /home/dockeruser/lgr/ Mirror name

example:

docker save 192.162.100.92:5000/my_pro/front_dev:1.1 > /home/dockeruser/lgr/
  • explain:

-o (i.e. output) or > means output to file;

/home/dockeruser/lgr/Specifies the exported location path. If the path is not specified, the default is the current folder;

  • Notice:

Generally, mirror names are used for export. If you use docker save -o /home/dockeruser/lgr/ mirror idExport (that is, use the image id to export). After importing this image, you will find that the name and tag of the image are none, so you also need to use docker tag to rename the image.

1.2 Docker image import

First, you need to use xftp or other tools to upload the above .tar file to the server that needs to be imported into the image.

  • Order:
docker load [options]
  • Use the command:
docker load -i 

or

docker load < 

example:

docker load <  /usr/local/epower/lgr/
  • explain:

-i (i.e. input) or < means input from a file;

Use the docker images command to check whether the import is successful. If the import is successful, the container can be run according to the image;

Since the load command cannot rename the image, we can use the docker tag image id image new name: the new tag command to rename the image;

Import and export of containers

2.1 Export of docker containers

  • Order:
docker export [options] container
  • Use the command:
docker export -o /home/dockeruser/lgr/ 0a64dafa5db9

explain:

  • -o means output to file;
  • /home/dockeruser/lgr/Specify the exported location path;
  • 0a64dafa5db9 is the container id;

2.2 Docker container import

First, you need to use xftp or other tools to upload the above .tar file to the server that needs to be imported into the image.

  • Order:
docker import [options] file|url| -[repository[:tag]]
  • Use the command:
docker import  epower:latest

explain:

  • Indicates the imported container;
  • epower:latest means rename the imported image, and latest means tag the imported image;

Use the docker images command to check whether the import is successful. If the import is successful, the container can be run according to the image;

3. Summary

  • Mirror import is a copying process;The import of the container is to turn the current container into a new image
  • The docker save command saves the image; the docker export command saves the container;
  • The tar file exported by the export command is slightly smaller than that exported by the save command;
  • The export command exports a container. When importing the export file, it cannot retain all the history of the image (i.e., layer information per layer), so it cannot be rolled back. The save command exports a mirror, so each layer of layer information can be completely preserved during import. The docker history mirror id command can query layer information for each layer.
  • The load command cannot rename the image; the import command can rename the image;

4. Use suggestions

If it is just for backup images, use the save and load commands;

If the container content needs to be replaced after the container has changed after it is started, use the export and import commands;

5. Encountering problems

  • question:

After exporting the container with the export command and import as a mirror, use the command

docker run -p 8812:8889 --name xxx -d 753 Failed to start the container,

The error is reported as follows:

docker: Error response from daemon: No command specified.

See 'docker run --help'.

The error prompts that the clear command is missing;

  • Solution:

On the server where the export container is exported, use the docker ps --no-trunc command to view the specific command, and then add the command to the end of the startup command.

docker run -p 8812:8889 --name xxx -d 753 /bin/sh -c 'java -=Asia/Shanghai -cp /app/resources:/app/classes:/app/libs/* '

The above is personal experience. I hope you can give you a reference and I hope you can support me more.