The Docker import command
docker import This command imports a container snapshot from a tar file or URL to create a new Docker image.
The difference between docker load in Vue 2,docker import A new image can be created from a container snapshot without preserving the image's history and metadata.
Syntax
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
file|URL|-: The path of the input file, a local file, or a URL, or use-to read from standard input.[REPOSITORY[:TAG]]: (Optional) Specify the repository and tag for the imported image.
OPTIONS description:
-c, --change: Apply Dockerfile instructions during the import process, such asCMD、ENTRYPOINT、ENV, etc.-m, --message: Add a comment to the imported image.
1. Import an image from a local tar file
docker import mycontainer.tar mynewimage:latest
This imports the image from the mycontainer.tar file and names it mynewimage:latest.
2. Import an image from a URL
docker import http://example.com/mycontainer.tar mynewimage:latest
This imports the image from the specified URL and names it mynewimage:latest.
3. Import an image from standard input
cat mycontainer.tar | docker import - mynewimage:latest
This reads the tar file from standard input via a pipe and imports the image.
4. Apply changes during the import process
docker import -c "ENV LANG=en_US.UTF-8" -c "CMD /bin/bash" mycontainer.tar mynewimage:latest
This imports the image from mycontainer.tar and sets the environment variable LANG and the command CMD during the process.
Example
1. Export a container snapshot
First, create and run a container:
docker run -d --name mycontainer ubuntu:20.04 sleep 3600
Export the container snapshot:
docker export mycontainer -o mycontainer.tar
2. Import the container snapshot
Import an image from a tar file:
docker import mycontainer.tar mynewimage:latest
3. Verify the imported image
docker images
Example output:
REPOSITORY TAG IMAGE ID CREATED SIZE
mynewimage latest 123abc456def 1 minute ago 72.9MB
4. Run the imported image
docker run -it mynewimage:latest /bin/bash
Notes
docker importThe created image does not preserve the history and metadata of the original image.- Use
-cThis option can apply Dockerfile instructions during the import process, thereby customizing the configuration of the new image. - The imported tar file must be a container snapshot created using
docker exportor another compatible format.
docker import This command is a flexible method for creating a new image from a container snapshot, suitable for scenarios such as migrating, restoring, and customizing Docker images. By using the docker import, users can easily generate a new image from a container snapshot and apply extra configuration during the import process.
Docker Command Reference