Skip to content

Instantly share code, notes, and snippets.

@daalvand
Last active July 31, 2023 09:18
Show Gist options
  • Select an option

  • Save daalvand/18c5a09fa844554f82bccc13d057ff82 to your computer and use it in GitHub Desktop.

Select an option

Save daalvand/18c5a09fa844554f82bccc13d057ff82 to your computer and use it in GitHub Desktop.
Backup Docker Images
#!/bin/bash
images_dir="$(dirname "$(readlink -f "$0")")/images"
mkdir -p $images_dir
#This awk command removes duplicates based on the last field ({{.ID}}) of the input lines by utilizing an associative array seen. It prints only the first occurrence of each unique ID
images=$(docker images --format "{{.Repository}}:{{.Tag}}-{{.ID}}" --filter "dangling=false" | awk -F'-' '!seen[$NF]++')
for image in $images; do
output_file="${image//[:\/]/_}.tar"
image_name=${image%-*}
echo $image_name
docker save -o "${images_dir}/${output_file}" "${image_name}"
done
#!/bin/bash
images_dir="$(dirname "$(readlink -f "$0")")/images"
for F in "${image_dir}/*.tar"; do
docker load < $F
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment