Docker import Command

Docker command reference


docker importThe command is used to import a container snapshot from a tar file or URL, thereby creating a new Docker image.

anddocker loadDifferent,docker importA new image can be created from a container snapshot without needing to preserve the image's history and metadata.

Syntax

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
  • file|URL|-: The path to the input file, a local file, or URL, or use-Read from standard input.
  • [REPOSITORY[:TAG]]: (Optional) Specify a repository and tag for the imported image.

OPTIONS description:

  • -c, --change: Apply Dockerfile directives during the import process, such asCMD、ENTRYPOINT、ENVetc.
  • -m, --message: Add comments to the imported image.

1. Import an image from a local tar file

docker import mycontainer.tar mynewimage:latest

This will import the image from the mycontainer.tar file and name it mynewimage:latest.

2. Import image from URL

docker import http://example.com/mycontainer.tar mynewimage:latest

This will import an image from the specified URL and name it mynewimage:latest.

3. Import image from standard input

cat mycontainer.tar | docker import - mynewimage:latest

This will read a tar file from standard input via a pipe and import the image.

4. Apply changes during import

docker import -c "ENV LANG=en_US.UTF-8" -c "CMD /bin/bash" mycontainer.tar mynewimage:latest

This will import an image from mycontainer.tar, setting the environment variable LANG and command CMD in the process.

Example

1. Export 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 container snapshot

Import image from tar file:

docker import mycontainer.tar mynewimage:latest

3. Verify the imported image

docker images

Output example:

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 will not preserve the original image's history and metadata.
  • Usage-cThe option can apply Dockerfile directives during the import process, thereby customizing the new image's configuration.
  • The imported tar file must be created usingdocker exporta container snapshot created, or another compatible format.

docker importThe command is a flexible method for creating new images from container snapshots, suitable for scenarios such as migrating, restoring, and customizing Docker images. By usingdocker importusers can easily generate new images from container snapshots and apply additional configuration during the import process.


Docker command reference

other extensions