Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker export with examples

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

CommandDescription
docker export -o my_container.tar my_containerExport a container to a tar file
`docker export my_containergzip > my_container.tar.gz`
`docker export my_containertar -xvf – -C /path`
docker export -o stopped_container.tar stopped_containerExport a stopped container
`docker export my_containerdocker import – my_new_image`
docker export -o /backups/db_backup.tar db_containerBackup a database container’s filesystem

Best Practices for Using docker export:

  1. Combine with docker import to create portable images from exported containers.
  2. Compress tarballs using tools like gzip or xz to save storage space.
  3. Use meaningful filenames when exporting containers (e.g., include timestamps).
  4. Verify exported tarballs by extracting them and inspecting the contents.
  5. Exclude sensitive data from exported containers before sharing or transferring.

Common Errors and Solutions

  1. “No such container”
    → Ensure the container exists and is running/stopped. Use docker ps -a to verify.
  2. “Permission denied”
    → Use elevated privileges (sudo) or ensure the current user has Docker permissions.
  3. “File size too large”
    → Compress the tarball during export (gzip or xz) or exclude unnecessary files before exporting.
  4. “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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x