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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
Transfer the file to another machine (e.g., using scp):
scp my_container.tar user@remote:/path/to/destination
Code language: JavaScript (javascript)
5. Combine docker export with docker import
Export the container:
docker export -o my_container.tar my_container
Code language: JavaScript (javascript)
Import the tarball as a new image:
docker import my_container.tar my_new_image:latest
Code language: CSS (css)
Run a container from the new image:
docker run -it my_new_image:latest bash
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
8. Use docker export with docker cp
Export the filesystem and extract only specific files:
docker export my_container | tar -xvf - ./path/to/file
Code language: JavaScript (javascript)
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 exportto 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 importto create portable images from exported containers. - Compress tarballs using tools like
gziporxzto 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 -ato verify. - “Permission denied”
โ Use elevated privileges (sudo) or ensure the current user has Docker permissions. - “File size too large”
โ Compress the tarball during export (gziporxz) 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
Code language: JavaScript (javascript)
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
Code language: CSS (css)
Extract Logs from an Exported Container
docker export my_container | tar -xvf - ./var/log
Code language: JavaScript (javascript)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals