[小技巧] Docker 备份Export和回复Import
date
Mar 8, 2023
slug
tip-docker-export-import-backup-restore
status
Published
summary
小技巧
tags
tip
type
Post
URL
Problem & Summary
For images - (静态)
Save
# all
docker save $(docker images -q) -o /path/to/save/mydockersimages.tar
# or some
# IDS=$(docker images | awk '{if ($1 ~ /^(debian|centos)/) print $3}')
# docker save $IDS -o /path/to/save/somedockersimages.tar
# save tags
docker images | sed '1d' | awk '{print $1 " " $2 " " $3}' > mydockersimages.list
Load
docker load -i /path/to/save/mydockersimages.tar
# then tg
while read REPOSITORY TAG IMAGE_ID
do
echo "== Tagging $REPOSITORY $TAG $IMAGE_ID =="
docker tag "$IMAGE_ID" "$REPOSITORY:$TAG"
done < mydockersimages.list
For containers - 动态
- This includes both the image files as well as any changes made while the container was running.
Export
docker export abc123 def456 > my_containers.tar
Import
cat my_containers.tar | docker import - my_image
#!/bin/bash
# List of containers to export
# containers=("webserver" "database" "appserver")
# Get a list of all running containers
containers=$(docker ps -q)
# Export each container
for container in "${containers[@]}"
do
docker export "$container" > "$container.tar"
done
# Create a new directory for imported containers
mkdir -p imported_containers
# Import each container
for container in "${containers[@]}"
do
cat "$container.tar" | docker import - "imported_containers/$container"
done