Docker containers have become an essential part of modern software development, streamlining the process of building, shipping, and running applications. In this article, we’ll explore how to export and import Docker containers, enabling you to move containers between systems and share them with other developers easily. This process is particularly useful for backing up containers, migrating them between hosts, or distributing pre-configured environments for consistent development and testing.

Advertisement

Prerequisites

To follow this tutorial, you’ll need the following:

  • Docker installed on your system. You can download it from the official Docker website (https://www.docker.com/) if you haven’t already.
  • Basic familiarity with Docker commands and concepts.

Exporting Docker Containers

Exporting a Docker container is the process of creating a portable archive file containing the container’s filesystem and configuration. This archive can then be imported into another Docker instance to recreate the container. To export a container, follow these steps:

  1. List your existing containers: Run the following command to list all the containers on your system, including their container IDs and names.
    docker ps -a 
    
  2. Stop the container: Before exporting, ensure the container is in a stopped state. Use the following command, replacing “CONTAINER_ID” with the actual container ID or name:
    docker stop CONTAINER_ID 
    
  3. Export the container: Use the docker export command to create a tar archive of the container’s filesystem. Replace “CONTAINER_ID” with the actual container ID or name, and “OUTPUT_FILE.tar” with the desired output file name:
    docker export CONTAINER_ID > OUTPUT_FILE.tar 
    

    This command will create a tar archive named “OUTPUT_FILE.tar” containing the container’s filesystem.

Importing Docker Containers

Now that you have the exported archive, you can import it into another Docker instance to recreate the container. Follow these steps to import a Docker container:

  1. Copy the archive file: If you’re importing the container to a different machine, transfer the tar archive to the target machine using a method of your choice (e.g., SCP, SFTP, or a USB drive).
  2. Import the container: Use the docker import command to create a new image from the tar archive. Replace “ARCHIVE_FILE.tar” with the actual file name, and “NEW_IMAGE_NAME” with the desired name for the new image:
    cat ARCHIVE_FILE.tar | docker import - NEW_IMAGE_NAME 
    

    This command will create a new Docker image with the specified name, containing the filesystem and configuration from the exported container.

  3. Run the imported container: To create a new container from the imported image, use the docker run command. Replace “NEW_IMAGE_NAME” with the actual image name and provide any necessary options, such as port mappings or volume mounts:
    docker run -d --name NEW_CONTAINER_NAME [OPTIONS] NEW_IMAGE_NAME 
    

    This command will create and start a new container based on the imported image.

Conclusion

In this article, we have covered the process of exporting and importing Docker containers. This process allows you to move containers between systems, share them with other developers, and ensure consistent environments for development and testing.

Keep in mind that exporting and importing containers using this method does not include the container’s runtime data, such as volumes or environment variables. To include this data, consider using Docker images and the docker save and docker load commands instead, or create a Dockerfile to build a new image with the desired configuration.

By mastering these techniques, you’ll be able to manage and distribute your containers more effectively, making your development process smoother and more efficient. As you continue to work with Docker, remember to explore its rich ecosystem of tools and resources, which can further enhance your container management and deployment capabilities.

Share.

7 Comments

  1. alvianno wijaya on

    root@DM03MAP02:~# docker run -i -t klokantech/openmaptiles-server /bin/bash
    root@081a4d1189d4:/#

    change root@*** and how to hosting countainer???

    • If u want to change root@something then u can specify with command #docker run -it –hostname=anything-here repositorynamehere(ex:-ubuntu:latest) /bin/bash

  2. Hi
    I use docker run -d -p 63700:80 –name apachetest –volumes-from apache3 apache , and export to tar file.
    When I import to different server , I lost all of settings , how do I backup the container parameter?
    thanks.

    • docker run -it -d yourrepo or anything else u want to attach with command at last dont forget to use command
      # docker commit your repositorynamehere

  3. This worked ok, but not all of the data has moved with the container.
    Do you have any further notes on how to copy underlying data?

Exit mobile version