0

when I execute docker images I have below docker images as list where there are images with multiple tag and also image with latest tag value.

REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
m1                                  latest              40febdb010b1        15 minutes ago      479MB
m2                                    130                 fab5a122127a        4 weeks ago         2.74GB
m2                                    115                 5a2ee5c5f5e5        4 weeks ago         818MB
m3                                    111                 dd91a7f68e3d        5 weeks ago         818MB
m3                                     23                  0657662756f6        5 weeks ago         2.22GB
m4                                     23                  0657662756f6        5 weeks ago         2.22GB

While I do for i in {docker image save -o <imagename>.tar} I would like to only save the images as tar for the tag with higher numbers but except any docker image with latest tag and docker image name as m4 How this can be achieved in one liner command.

1 Answer 1

0

Assuming bash, here’s a “one-liner” that does this:

unset saved; declare -A saved; docker images --format '{{.Repository}} {{.Tag}}' | while read repo tag; do if [[ "$repo" == "m4" ]] || [[ "$tag" == latest ]]; then continue; fi; if [[ "${saved[$repo]}" != true ]]; then docker image save -o "$repo-$tag".tar "$repo:$tag"; saved["$repo"]=true; fi; done

Split up so it’s understandable:

unset saved
declare -A saved
docker images --format '{{.Repository}} {{.Tag}}' | while read repo tag; do
  if [[ "$repo" == "m4" ]] || [[ "$tag" == latest ]]; then continue; fi
  if [[ "${saved[$repo]}" != true ]]; then
    docker image save -o "$repo-$tag".tar "$repo:$tag"
    saved["$repo"]=true
  fi
done

This declares an associative array saved, lists the images with only their repository and tag, skips latest images, and saves the image if it hasn’t already saved the repository. To determine the latter, when an image is saved, that fact is recorded in the saved array; before saving an image, the array is checked to see whether an image with the same repository has already been saved.

docker images lists the images starting with the most recent one, so this will save the most recent image whenever there are multiple images sharing the same repository (or name).

There’s no special processing of the tarball name so it might not do what you’re after with repositories containing slashes. There’s also no handling of images with no repository or tag. The following longer version creates subdirectories as appropriate and skips unlabeled images:

unset saved
declare -A saved
docker images --format '{{.Repository}} {{.Tag}}' | while read repo tag; do
  if [[ "$repo" == "m4" ]] || [[ "$tag" == latest ]] || [[ "$repo" == "<none>" ]] || [[ "$tag" == "<none>" ]]; then continue; fi
  if [[ "${saved[$repo]}" != true ]]; then
    if [[ "${repo}" =~ "/" ]]; then mkdir -p "${repo%/*}"; fi
    docker image save -o "$repo-$tag".tar "$repo:$tag"
    saved["$repo"]=true
  fi
done
5
  • Thanks, but would it always save the image with higher tag number ? also as I mentioned I would need to skip any image with repo name m4 here is my desired output (tars which should be saved) : m2-130.tar , m2-111.tar
    – Alex
    Commented Oct 27, 2021 at 7:51
  • When multiple images share a name, this will save the most recent one. I missed the m4 requirement, I’ll fix that. Commented Oct 27, 2021 at 7:56
  • Also I did not understand the context of this if [[ "${saved[$repo]}" != true ]] will this check if the image.tar already exists in current folder and if not then it will save ?
    – Alex
    Commented Oct 27, 2021 at 7:56
  • See the update. Commented Oct 27, 2021 at 8:03
  • Thanks got it, it works now
    – Alex
    Commented Oct 27, 2021 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .