Here’s a complete tutorial on docker export
, covering what it does, examples, and use cases.
What is docker export
?
docker export
is a Docker command used to export the filesystem of a container as a tarball. This allows you to save the container’s data as a single file, which can then be imported or extracted later.
Key Features:
- Exports the filesystem of a container, excluding image layers and Docker metadata.
- Useful for creating backups or transferring containers to other systems.
- Does not preserve container configuration (e.g., environment variables, ports).
Basic Syntax
docker export [OPTIONS] CONTAINER
Options:
-o
,--output
: Write the output to a file instead of standard output.
Examples of docker export
1. Export a Container’s Filesystem to a Tar File
docker export -o my_container.tar my_container
This saves the filesystem of my_container
as a tarball named my_container.tar
.
2. Export a Container and Pipe it to Another Command
docker export my_container | gzip > my_container.tar.gz
This compresses the exported filesystem of my_container
and saves it as my_container.tar.gz
.
3. Extract Files from the Exported Tarball
Export the container:
docker export -o my_container.tar my_container
Extract the tarball contents:
tar -xvf my_container.tar -C /path/to/extract
4. Export and Transfer the Container to Another Machine
Export the container:
docker export -o my_container.tar my_container
Transfer the file to another machine (e.g., using scp
):
scp my_container.tar user@remote:/path/to/destination
5. Combine docker export
with docker import
Export the container:
docker export -o my_container.tar my_container
Import the tarball as a new image:
docker import my_container.tar my_new_image:latest
Run a container from the new image:
docker run -it my_new_image:latest bash
6. Export Multiple Containers Using a Script
#!/bin/bash
for container in $(docker ps -q); do
docker export -o ${container}.tar $container
echo "Exported $container"
done
This script exports the filesystems of all running containers to separate tar files.
7. Export a Stopped Container
You can export stopped containers as well:
docker export -o stopped_container.tar stopped_container
8. Use docker export
with docker cp
Export the filesystem and extract only specific files:
docker export my_container | tar -xvf - ./path/to/file
Use Cases for docker export
1. Backup and Restore
- Export the entire filesystem of a container for backup purposes.
- Example: Backup the filesystem of a database container for disaster recovery.
2. Transfer Containers Across Environments
- Use
docker export
to transfer container filesystems to other environments or systems. - Example: Export a container from a development machine and import it into a production system.
3. Debugging and Inspection
- Export the container filesystem to analyze or debug its contents.
- Example: Extract configuration files or logs for debugging purposes.
4. Reduce Image Size for Specific Use Cases
- Export a container, remove unnecessary files, and re-import it as a lighter image.
- Example: Create a minimal base image from a container.
5. Sharing Application State
- Export a container with pre-installed applications or dependencies for easy sharing.
List of Common docker export
Commands
Command | Description |
---|---|
docker export -o my_container.tar my_container | Export a container to a tar file |
`docker export my_container | gzip > my_container.tar.gz` |
`docker export my_container | tar -xvf – -C /path` |
docker export -o stopped_container.tar stopped_container | Export a stopped container |
`docker export my_container | docker import – my_new_image` |
docker export -o /backups/db_backup.tar db_container | Backup a database container’s filesystem |
Best Practices for Using docker export
:
- Combine with
docker import
to create portable images from exported containers. - Compress tarballs using tools like
gzip
orxz
to save storage space. - Use meaningful filenames when exporting containers (e.g., include timestamps).
- Verify exported tarballs by extracting them and inspecting the contents.
- Exclude sensitive data from exported containers before sharing or transferring.
Common Errors and Solutions
- “No such container”
→ Ensure the container exists and is running/stopped. Usedocker ps -a
to verify. - “Permission denied”
→ Use elevated privileges (sudo
) or ensure the current user has Docker permissions. - “File size too large”
→ Compress the tarball during export (gzip
orxz
) or exclude unnecessary files before exporting. - “Exported tarball is incomplete”
→ Verify the container was running/stopped properly before export.
Combining docker export
with Other Commands
Backup and Restore a Container
Export the container:
docker export -o db_backup.tar db_container
Transfer the tarball to another system and re-import it as an image:
docker import db_backup.tar db_image:latest
docker run -it db_image:latest bash
Extract Logs from an Exported Container
docker export my_container | tar -xvf - ./var/log
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND