dockerTools: fix image json and manifest

The image json is not exactly the same as the layer json, therefore I
changed the implementation to use the `baseJson` which doesn’t include
layer specific details like `id`, `size` or the checksum of the layer.

Also the `history` entry was missing in the image json. I’m not totally
sure if this field is required, but a I got an error from a docker
registry when I’ve tried to receive the distribution manifest of an
image without those `history` entry:

GET: `http://<registry-host>/v2/<imageName>/manifests/<imageTag>`

```json
{
"errors": [
{
"code": "MANIFEST_INVALID",
"message": "manifest invalid",
"detail": {}
}
]
}
```

I’ve also used a while loop to iterate over all layers which should make
sure that the order of the layers is correct. Previously `find` was
used and I’m not sure if the order was always correct.

authored by Mathias Schreck and committed by Robin Gloster 86d9b09c f6817ddf

+16 -9
+16 -9
pkgs/build-support/docker/default.nix
··· 497 497 # Use the temp folder we've been working on to create a new image. 498 498 mv temp image/$layerID 499 499 500 - # Create image configuration file (used by registry v2) by using 501 - # the configuration of the last layer 502 - SHA_ARRAY=$(find ./ -name layer.tar | xargs sha256sum | cut -d" " -f1 | xargs -I{} echo -n '"sha256:{}" ' | sed 's/" "/","/g' | awk '{ print "["$1"]" }') 503 - jq ". + {\"rootfs\": {\"diff_ids\": $SHA_ARRAY, \"type\": \"layers\"}}" image/$layerID/json > config.json 504 - CONFIG_SHA=$(sha256sum config.json | cut -d ' ' -f1) 505 - mv config.json image/$CONFIG_SHA.json 500 + # Create image json and image manifest 501 + imageJson=$(cat ${baseJson} | jq ". + {\"rootfs\": {\"diff_ids\": [], \"type\": \"layers\"}}") 502 + manifestJson=$(jq -n "[{\"RepoTags\":[\"$imageName:$imageTag\"]}]") 503 + currentID=$layerID 504 + while [[ -n "$currentID" ]]; do 505 + layerChecksum=$(sha256sum image/$currentID/layer.tar | cut -d ' ' -f1) 506 + imageJson=$(echo "$imageJson" | jq ".history |= [{\"created\": \"${created}\"}] + .") 507 + imageJson=$(echo "$imageJson" | jq ".rootfs.diff_ids |= [\"sha256:$layerChecksum\"] + .") 508 + manifestJson=$(echo "$manifestJson" | jq ".[0].Layers |= [\"$currentID/layer.tar\"] + .") 509 + 510 + currentID=$(cat image/$currentID/json | (jshon -e parent -u 2>/dev/null || true)) 511 + done 506 512 507 - # Create image manifest 508 - LAYER_PATHS=$(find image/ -name layer.tar -printf '"%P" ' | sed 's/" "/","/g') 509 - jq -n "[{\"Config\":\"$CONFIG_SHA.json\",\"RepoTags\":[\"$imageName:$imageTag\"],\"Layers\":[$LAYER_PATHS]}]" > image/manifest.json 513 + imageJsonChecksum=$(echo "$imageJson" | sha256sum | cut -d ' ' -f1) 514 + echo "$imageJson" > "image/$imageJsonChecksum.json" 515 + manifestJson=$(echo "$manifestJson" | jq ".[0].Config = \"$imageJsonChecksum.json\"") 516 + echo "$manifestJson" > image/manifest.json 510 517 511 518 # Store the json under the name image/repositories. 512 519 jshon -n object \