remove-onbuild.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash
  2. set -e
  3. # do some hacks to strip out onbuilds in a new faked layer
  4. # usage: ./remove-onbuild.sh input-image output-image
  5. # ie: ./remove-onbuild.sh rails:onbuild librarytest/rails-onbuild-without-onbuild
  6. in="$1"; shift
  7. out="$1"; shift
  8. outImage="${out%%:*}"
  9. outTag="${out#*:}"
  10. [ "$outImage" != "$outTag" ] || outTag='latest'
  11. tmp="$(mktemp -t -d docker-library-test-remove-onbuild-XXXXXXXXXX)"
  12. trap "rm -rf '$tmp'" EXIT
  13. declare -A json=(
  14. [Size]=0
  15. )
  16. declare -A mappings=(
  17. [parent]='.Id'
  18. [created]='.Created'
  19. [container]='.Container'
  20. [container_config]='.ContainerConfig'
  21. [config]='.Config'
  22. [docker_version]='.DockerVersion'
  23. [architecture]='.Architecture'
  24. [os]='.Os'
  25. )
  26. for key in "${!mappings[@]}"; do
  27. val="$(docker inspect -f '{{json '"${mappings[$key]}"'}}' "$in")"
  28. json["$key"]="$val"
  29. done
  30. onbuildConfig="$(docker inspect -f '{{json .Config.OnBuild}}' "$in" | sed 's/[]\/$*.^|[]/\\&/g')" # pre-escaped for use within "sed"
  31. json[config]="$(echo "${json[config]}" | sed -r "s/$onbuildConfig/null/g")" # grab the image config, but scrub the onbuilds
  32. jsonString='{'
  33. first=1
  34. for key in "${!json[@]}"; do
  35. [ "$first" ] || jsonString+=','
  36. first=
  37. jsonString+='"'"$key"'":'"${json[$key]}"
  38. done
  39. newId="$(echo "$jsonString" | sha256sum | cut -d' ' -f1)" # lol, this is hacky
  40. jsonString+=',"id":"'"$newId"'"}'
  41. mkdir -p "$tmp/$newId"
  42. echo "$jsonString" > "$tmp/$newId/json"
  43. echo -n '1.0' > "$tmp/$newId/VERSION"
  44. dd if=/dev/zero of="$tmp/$newId/layer.tar" bs=1k count=1 &> /dev/null # empty tar file
  45. cat > "$tmp/repositories" <<EOF
  46. {"$outImage":{"$outTag":"$newId"}}
  47. EOF
  48. docker rmi -f "$out" &> /dev/null || true # avoid "already exists, renaming the old one" from "docker load"
  49. tar -cC "$tmp" . | docker load