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.](https://phoenixnap.com/kb/wp-content/uploads/2024/02/docker-export-command-how-to-export-containers.png)
Prerequisites
- Docker installed (see our Install Docker on Ubuntu and Install Docker on Rocky Linux tutorials).
- Command-line access.
- Administrative privileges.
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
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.
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
, andWORKDIR
.-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.