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 export command

The docker export command allows you to export a container's filesystem as a tar archive. This is useful when you want to create a backup of the container, share its filesystem, or migrate the container to another system. In this tutorial, we will walk through the basics of using the docker export command.

  1. Prerequisites:

    • Install Docker on your system
    • Have a container that you want to export. If you don't have one, you can use the following example command to create a container:
    docker run -it --name example-container ubuntu:20.04 /bin/bash
    

    This command will start a new container based on the ubuntu:20.04 image and open an interactive shell session. The --name flag is used to assign a name to the container for easier reference.

  2. Export the container filesystem:

    To export the container's filesystem as a tar archive, use the following syntax:

    docker export CONTAINER > output.tar
    

    Replace CONTAINER with the container ID or name, and output.tar with the desired output file name.

    For example, to export the example-container filesystem as a tar archive named example-container.tar, use:

    docker export example-container > example-container.tar
    

    This command will create a tar archive in the current directory containing the container's filesystem.

  3. Inspect the exported filesystem:

    You can extract the contents of the tar archive to inspect the exported filesystem using the tar command:

    mkdir extracted-container
    tar -xf example-container.tar -C extracted-container
    

    This will extract the contents of example-container.tar into a new directory named extracted-container. You can now inspect the files and directories that were part of the container's filesystem.

  4. Import the exported filesystem:

    If you want to import the exported filesystem as a new container or image, you can use the docker import command. For example, to import the example-container.tar as a new image, use:

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

    This will create a new image named example-image based on the exported filesystem. You can then run a new container using this image:

    docker run -it --name new-container example-image /bin/bash
    

In this tutorial, we covered the basics of using the docker export command to export a container's filesystem as a tar archive. The exported filesystem can be used to create backups, share data, or migrate containers between systems. Remember that the docker export command only exports the container's filesystem, not the metadata or the configuration settings of the container.

  1. How to Use Docker Export Command:

    • Description: The docker export command allows you to export a container's filesystem as a tarball, which can be useful for creating backups or sharing container filesystems.
    • Code Example:
      docker export <container_id_or_name> > container_export.tar
      
  2. Creating Tarballs with Docker Export:

    • Description: Docker export generates a tarball containing the entire filesystem of a container, which can be compressed for efficiency.
    • Code Example:
      docker export <container_id_or_name> | gzip > container_export.tar.gz
      
  3. Docker Export Command Examples:

    • Description: Various examples showcasing different use cases of the docker export command.
    • Code Examples:
      docker export -o container_export.tar <container_id_or_name>
      docker export --output container_export.tar <container_id_or_name>
      
  4. Exporting Docker Container Filesystem:

    • Description: Docker export captures the filesystem of a running or stopped container, allowing you to save it for later use.
    • Code Example:
      docker export <container_id_or_name> > container_export.tar
      
  5. Importing Exported Containers with Docker:

    • Description: Use docker import to create a new image from an exported tarball, allowing you to reuse or share container filesystems.
    • Code Example:
      docker import container_export.tar my_imported_image