Skip to content

Instantly share code, notes, and snippets.

@crazycodr
Created January 18, 2018 03:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazycodr/7656082e2180e11399037c752e81ed8e to your computer and use it in GitHub Desktop.
Save crazycodr/7656082e2180e11399037c752e81ed8e to your computer and use it in GitHub Desktop.
EFS docker image cache
#!/bin/bash
set -euo pipefail
docker_image_manifest_directory="$(mktemp -d "docker-image-manifest.XXXX")"
docker inspect $(docker images -q) \
| jq '[.[] | { id: .Id, tags: .RepoTags[] | split(":") } | { id: .id, repository: .tags[0], version: .tags[1] } ] | unique' \
> "${docker_image_manifest_directory}/image.manifest"
cp /efs/build-cache/docker-images/cache.manifest "${docker_image_manifest_directory}/cache.manifest"
# Returns nothing since both files are the same
# jq --slurp '.[0] - .[1]' image.manifest cache-exact.manifest
#
# Returns nothing since missing items are in docker
# jq --slurp '.[0] - .[1]' image.manifest cache-missing.manifest
#
# Returns nothing since superfluous items are in cache
# jq --slurp '.[0] - .[1]' image.manifest cache-superfluous.manifest
#
# Returns items not in cache
# jq --slurp '.[0] - .[1]' image.manifest cache-different.manifest
# jq --slurp '.[0] - .[1]' image.manifest cache-missing.manifest
#
# Returns items not in docker
# jq --slurp '.[1] - .[0]' image.manifest cache-different.manifest
# jq --slurp '.[1] - .[0]' image.manifest cache-superfluous.manifest
# To add missing items into cache, i need to diff 0 and 1 for items missing in cache
# These items are re-analyzed by jq to output a key value pair that a while read stores into variables
# Then, for each item that i read out, i can call docker save and then modify the cache.manifest to merge
# the data in
while read -r image_id; read -r image_repository; read -r image_version; do
docker save "${image_repository}:${image_version}" --output "$(echo "${docker_image_manifest_directory}/${image_id}" | sed "s/sha256://").tar"
jq --slurp '.[0] + .[1]'
done <<< "$(jq --slurp '.[0] - .[1]' \
"${docker_image_manifest_directory}/image.manifest" \
"${docker_image_manifest_directory}/cache.manifest" \
| jq '.[] | .id, .repository, .version' -r)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment