Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker import command

The docker import command allows you to create a Docker image from a tarball of a filesystem. This can be useful when you want to convert a filesystem backup, a virtual machine image, or another system's filesystem into a Docker image. In this tutorial, we will walk through the basics of using the docker import command.

  1. Prerequisites:

    • Install Docker on your system
    • Have a tar archive of a filesystem that you want to import. For example, you can use the example-container.tar created in the "Docker export command tutorial".
  2. Import the filesystem as a Docker image:

    To import a tarball of a filesystem as a Docker image, use the following syntax:

    cat FILESYSTEM_TAR | docker import - IMAGE_NAME[:TAG]
    

    Replace FILESYSTEM_TAR with the path to the tarball of the filesystem, and IMAGE_NAME[:TAG] with the desired image name and optional tag.

    For example, to import the example-container.tar as a new image named imported-image, use:

    cat example-container.tar | docker import - imported-image
    

    This will create a new image named imported-image based on the imported filesystem.

  3. List the imported image:

    To check if the imported image has been added to your local images, use the docker images command:

    docker images
    

    You should see the imported-image in the list of images.

  4. Run a container from the imported image:

    To run a container from the imported image, use the docker run command:

    docker run -it --name imported-container IMAGE_NAME[:TAG] COMMAND
    

    Replace IMAGE_NAME[:TAG] with the image name and optional tag, and COMMAND with the command to run inside the container.

    For example, to run a new container named imported-container from the imported-image image and start a shell session, use:

    docker run -it --name imported-container imported-image /bin/bash
    

    This will start a new container with the filesystem and data from the imported image.

In this tutorial, we covered the basics of using the docker import command to create a Docker image from a tarball of a filesystem. This can be useful for creating Docker images from system backups, virtual machine images, or other sources. Remember that the docker import command only imports the filesystem, not the metadata or the configuration settings of the original container or system. You may need to configure the imported image to match your desired settings.

  1. How to Use Docker Import Command:

    • Description: The docker import command creates a Docker image from a tarball archive containing a container's filesystem.
    • Code Example:
      docker import <tarball_path> <image_name>:<tag>
      
  2. Importing Docker Container Filesystems:

    • Description: Docker import allows you to import a container's filesystem into a new Docker image, preserving its structure and contents.
    • Code Example:
      docker export <container_id> > container_export.tar
      docker import container_export.tar <image_name>:<tag>
      
  3. Docker Import Command Examples:

    • Description: Various examples showcasing different use cases of the docker import command.
    • Code Examples:
      docker import container_export.tar <image_name>:<tag>
      docker import http://example.com/container_export.tar <image_name>:<tag>
      
  4. Creating Docker Images with Import Command:

    • Description: Docker import is a way to create Docker images from existing filesystems, allowing for customization before use.
    • Code Example:
      docker import --change "CMD ['nginx', '-g', 'daemon off;']" container_export.tar <image_name>:<tag>
      
  5. Importing Images into Docker with Import:

    • Description: Docker import is a way to bring external images into Docker, useful for scenarios where Docker Hub or other registries are not accessible.
    • Code Example:
      docker import http://example.com/image_export.tar <image_name>:<tag>
      
  6. Importing Docker Container Snapshots:

    • Description: Use Docker import to snapshot the filesystem of a running or stopped container, creating a reusable image.
    • Code Example:
      docker export <container_id> > container_export.tar
      docker import container_export.tar <image_name>:<tag>
      
  7. Docker Import and Data Migration:

    • Description: Docker import can be used for data migration by exporting container data and importing it into a new image.
    • Code Example:
      docker export <container_id> > container_export.tar
      docker import container_export.tar <image_name>:<tag>
      
  8. Docker Import and Exported Containers:

    • Description: Docker import is often used in conjunction with docker export to create images from exported container filesystems.
    • Code Example:
      docker export <container_id> > container_export.tar
      docker import container_export.tar <image_name>:<tag>
      
  9. Importing External Data into Docker Containers:

    • Description: Docker import allows you to bring external data into Docker images, providing a way to package and distribute custom configurations or datasets.
    • Code Example:
      docker import http://example.com/mydata.tar <image_name>:<tag>