🚒 Day 09: Docker Export/Import & Save/Load Guide

Ibrar AnsariIbrar Ansari
6 min read

Welcome to this hands-on guide for Exporting, Importing, Saving, and Loading Docker Containers and Images. Perfect for backups, sharing, or migrating environments! 🎯

🐳 Docker Image & Container Management Commands Summary

  • πŸ›¬ docker import – Creates an image from a tarball (e.g., from docker export).

  • πŸ“¦ docker load – Loads an image from a tar archive (e.g., from docker save).

  • πŸ›« docker export – Exports a container’s filesystem as a tar archive.

  • πŸ’Ύ docker save – Saves one or more images to a tar archive.

  • πŸ“ docker commit – Creates a new image from a container’s changes.


🎬 Video Demonstration

Watch on Youtube

βš–οΈ Comparison: docker export vs docker save vs docker commit

Featuredocker exportdocker savedocker commit
Operates onContainersImagesContainers
Includes image history❌ Noβœ… Yes❌ No (new image has no prior history)
Includes environment metadata❌ Noβœ… Yesβœ… Partial (CMD, ENV if set in container or base image)
Volume data❌ Not included❌ Not included❌ Not included
Layered image❌ No (flattened)βœ… Yesβœ… New image layer added
OutputContainer filesystem archiveFull Docker image archiveDocker image (tagged)
Typical useShare/backup container FSBackup/share full imageCapture container state as image

βš–οΈ Difference Between docker import vs docker load

Featuredocker importdocker load
πŸ“¦ Input TypeContainer filesystem archive (.tar)Docker image archive (.tar)
πŸ“š Metadata (e.g. CMD, ENV)❌ Not preservedβœ… Fully preserved
🏷️ Tags Image?Optional in commandUses tags from saved image
🧱 Layers Retained❌ No (flattened image, no layers/history)βœ… Yes (preserves image layers)
πŸ” Use CaseRebuild image from exported containerRestore image saved with docker save
πŸ”§ Common UsageBackup/migrate containers, minimal rebuildFull image backup/restore across systems

🧠 TL;DR

  • Use docker import if you have an exported **container filesystem** (export`).

  • Use docker **load** if you have a saved image archive (save).

  • Use docker **commit** if you want to turn a live container into a new image, preserving its current state.

πŸ”§ Prepare Environment for the Demo

# Run Day 08 Container for demo
DAY=day09
mkdir -p $DAY && cd $DAY 
echo "Welcome to DevOps in Action" | sudo tee ./index.html
docker run -itd --name=$DAY -p 8085:80 -v /home/user/$DAY:/usr/share/nginx/html nginx:alpine
http://192.168.1.22:8085/

# Example container ID or name
container_name="day09"
or 
container_name="d40fa919ae31"

πŸ“¦ 1. Exporting and Importing Containers

## πŸ”Ή Export a Running or Paused Container
# You can export a container's filesystem into a `.tar` archive.
docker export $container_name | gzip > $container_name.tgz
# πŸ“ This only includes the filesystem, not image layers or history.

## πŸ”Ή Import an Exported Container
gunzip -c $container_name.tgz | docker import - mynewimage:latest
docker image history mynewimage
# > πŸ” This creates a new image from the tarball, useful for backups or transferring containers.
# Test image
docker run -itd --name=testimage -p 8090:80 mynewimage:latest
# you got error here so see above table for understanding.
# πŸ“š Metadata (e.g. CMD, ENV) ❌ Not preserved

## πŸ”Ή Commit a Container to Preserve State with Metadata
docker commit -a "Ansari" $container_name preserve:v1
docker image history preserve:v1
# πŸ“ This creates a new image named 'preserve:v1' from the current state of the container.
#     - Includes the current filesystem state
#     - Retains metadata like CMD, ENV if the base image had it
#     - Useful for turning a modified container into a reusable image

## πŸ”Ή Run a Container from the Committed Image
docker run -itd --name=testimage -p 8090:80 preserve:v1
docker logs testimage
http://192.168.1.22:8090/

# 🟒 This runs a container from the newly committed image.

🧊 2. Saving and Loading Docker Images

## πŸ”Ή Save Docker Image to `.tar`
docker save -o ubuntu_latest.tar ubuntu:latest
ls -alsh ubuntu*
# Or compressed:
docker save ubuntu:latest | gzip > ubuntu_latest.tar.gz
du -sh ubuntu*

## πŸ”Ή Load Docker Image from `.tar`
docker image remove ubuntu:latest
docker images | grep ubuntu

docker load -i ubuntu_latest.tar
docker images | grep ubuntu
# Or for `.gz`:
docker image remove ubuntu:latest
docker load < ubuntu_latest.tar.gz
docker images | grep ubuntu

docker image history ubuntu

πŸ” Visual Analogy

ActionAnalogy
docker importπŸ“Έ Taking a photo of a container
docker load🧬 Cloning the entire image DNA

βš™οΈ 3. Exporting All Stopped Containers

Let’s export all exited containers with their names and IDs.

# First clean folder
rm -rf *

# Make a stopped container
docker stop $container_name

# Run hello world
docker run hello-world

# Exporting All Stopped Containers
while read image container_id; do
  image_sanitized=$(echo "$image" | tr '/:' '_') # handles slashes and tags
  docker export "$container_id" > "${image_sanitized}-${container_id}.tar"
done < <(docker ps -a -f status=exited --format '{{.Image}} {{.ID}}')

πŸš€ 4. Import Containers as Versioned Images

To convert exported containers into images with version tags:

for file in *.tar; do
  base=$(basename "$file" .tar)
  # e.g., "hello-world-53af1e9021bf"
  image_name="${base%-*}"      # remove the last dash + ID
  # container_id="${base##*-}"   # extract only the container ID
  # Optional: replace underscores back to slashes/colons if needed
  # image_name=$(echo "$image_name" | tr '_' '/')
  # docker import "$file" "${image_name}:${container_id}"
  docker import "$file" "${image_name}:latest"
done

🏷️ 5. Save All Images to .tar

Want to back up all images?

# First clean folder
rm -rf *

# Save All Images to `.tar`
docker save $(docker images | sed '1d' | awk '{print $1 ":" $2 }') -o allinone.tar

# To avoid malformed names dangling images if you have:
docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>" | xargs docker save -o allinone.tar

# Export it seprately
docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>" | while read image; do
  filename=$(echo "$image" | sed 's/[\/:]/_/g').tar
  docker save "$image" -o "$filename"
done

πŸ“€ 6. Load All Saved Images

Load every .tar file in the current directory:

# Stop all containers
docker rm -f $(docker ps -aq)

# Remove all images
docker rmi -f $(docker images -q)

# Load Saved Images
docker load -i allinone.tar

# Load All Saved Images seprately
for tarfile in *.tar; do
  echo "Loading $tarfile..."
  docker load -i "$tarfile"
done

# Verify it
docker images

# Test image
docker run -itd --name=test -p 8080:80 nginx:alpine

# Test Nginx
http://192.168.1.22:8080/

πŸ› οΈ 7. Troubleshooting

  • Error: No such container:

    • Ensure the container exists (docker ps -a).
  • Error: Invalid tar header:

    • Verify the tarball isn’t corrupted (tar -tvf file.tar).
  • Large tarball sizes:

    • Use compression (gzip) or clean up unnecessary container data.
  • Imported image not running:

    • Check if the image requires specific entrypoint/cmd (docker inspect).

πŸ’‘ Best Practices & Tips

Tip 🧠Description
Use compressionAlways gzip large exports to save space
Tag wiselyUse clear names & tags for imported images
Automate backupsUse cron or scripts to automate periodic saves
Combine with registriesFor team environments, push to private Docker registries instead
Verify after importUse docker run -it <image> /bin/bash to inspect restored containers

πŸ”— Additional Resources

0
Subscribe to my newsletter

Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ibrar Ansari
Ibrar Ansari

Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!