docker export Command: How to Export Containers

March 4, 2024

Introduction

Exporting a Docker container comes in handy when developing and testing resource-intensive applications. Rebuilding a large app from scratch for every iteration can be time-consuming, so docker export provides a way to create a snapshot of the running app.

This article shows how to export Docker container contents to an archive and import it on another system.

The docker export command: how to export Docker containers.

Prerequisites

docker export Command

Note: The docker export command is an alias of docker container export. The two commands are interchangeable.

docker export provides a way to archive and distribute a container filesystem to another machine. The command records the current state of the filesystem to a container image and exports the image as an archive.

The basic syntax for the command is:

docker export [container-name-or-id] > [filename].tar

The command redirects the standard output (STDOUT) to a TAR file.

docker export Options

The only command option for docker export is -o or --output. Add this option to the command to skip writing to STDOUT and write directly to a file:

docker export -o [filename].tar [container-name-or-id]

The command provides no output.

Note: The docker export command does not export the contents of Docker volumes attached to the container at the moment of archiving.

docker export Examples

The docker export command allows exporting to an uncompressed tarball and a compressed TAR.GZ file. The sections below provide examples of the two export operations.

Save Docker Container to Tarball

To identify the container for archiving, use its ID or name. Both identifiers are visible in the list of containers when you run the following command:

docker ps -a
Viewing a list of containers on the system.

The example above shows a container named app-container with the ID b0ef23dfe352. Export it to the test-app.tar archive by typing:

docker export -o app-container test-app.tar

Alternatively, use the container ID:

docker export b0ef23dfe352 > test-app.tar

Save Docker Container to Compressed Archive with gzip

Compress the target archive by piping the output to the gzip command. The following is the syntax for this operation:

docker export [container-name-or-id] | gzip > [filename].tar.gz

For example, to compress the container named app-container to the test-app.tar.gz archive, enter the following:

docker export app-container | gzip > test-app.tar.gz

The command does not provide output.

Note: The docker export command is similar to docker save. However, while docker export creates container snapshots and does not preserve the source history and metadata, docker save archives images and includes the related data in the archive.

Import Container Snapshot with docker import

To import the exported container filesystem as an image on another machine, type:

docker import [filename]

The command creates a new, untagged image.

Importing an image based on a container snapshot.

Alternatively, pipe the standard input (STDIN) to docker import:

cat [filename] | docker import - [new-image-name]:[tag]

The example below pipes the contents of test-app.tar to create app-snapshot:new, an image that contains the exported container snapshot.

cat test-app.tar | docker import - app-snapshot:new

docker import Options

The following options can be used to modify the main docker import command:

  • -c or --change provides a way to include a Dockerfile instruction to the command. The option supports the following instructions:  CMD, ENTRYPOINT, ENV, EXPOSE, ONBUILD, USER, VOLUME, and WORKDIR.
  • -m or --message allows the user to leave a commit message.
  • --platform sets the platform on multi-platform servers.

Conclusion

After reading this article, you should know how to export the contents of a Docker container to an archive. The article introduced the docker export command and showed how to use docker import to transfer the exported image to another machine.

Learn more about manipulating Docker containers by reading How to SSH into a Docker Container.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Docker Commands Cheat Sheet
December 7, 2022

Whether you are new to Docker or have experience with containerization, it is always good...
Read more
How to Use Docker Run Command
July 11, 2024

There are many different ways to run a container. By adding attributes to the basic syntax...
Read more
How To Install Docker on Ubuntu
April 6, 2023

In this tutorial, you will learn to install and use Docker on Ubuntu 20.04 or Ubuntu 22.04.
Read more
docker commit Explained
February 14, 2024

In this tutorial, you will learn how to use docker commit to create an image based on a container.
Read more