Skip to content

Instantly share code, notes, and snippets.

@rchaganti
Created November 28, 2022 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rchaganti/3915a1eda0a4b89adf39b80311d7c840 to your computer and use it in GitHub Desktop.
Save rchaganti/3915a1eda0a4b89adf39b80311d7c840 to your computer and use it in GitHub Desktop.
# Create a tar gz of the layer
cd ~/hello-cloud/imageLayer
tar -czvf ../hello-cloud.tar.gz *
cd ~/hello-cloud
# Obtain digests and diff IDs
LAYERDIGEST=$(sha256sum < hello-cloud.tar.gz | cut -d' ' -f1)
LAYERDIFF=$(gunzip < hello-cloud.tar.gz | sha256sum | cut -d' ' -f1)
LAYERSIZE=$(ls -l hello-cloud.tar.gz | awk '{print $5}')
# Create configuration.json
cat <<EOF > configuration.json
{
"architecture": "amd64",
"os": "linux",
"config": {
"Env": [
"PATH=/bin"
],
"Entrypoint": [
"hello-cloud"
],
"Cmd": [
"Cloud Native Central!"
]
},
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:${LAYERDIFF}"
]
},
"history": [
{
"created_by": "Ravikanth C"
}
]
}
EOF
CONFIGDIGEST=$(sha256sum < configuration.json | cut -d' ' -f1)
CONFIGSIZE=$(ls -l configuration.json | awk '{print $5}')
# Generate manifest.json
cat << EOF > manifest.json
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"config": {
"mediaType": "application/vnd.oci.image.config.v1+json",
"size": $CONFIGSIZE,
"digest": "sha256:$CONFIGDIGEST"
},
"layers": [
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"size": $LAYERSIZE,
"digest": "sha256:$LAYERDIGEST"
}
]
}
EOF
# Authenticate and get token
USERNAME=ravikanth
REPO="${USERNAME}/hello-cloud"
TOKEN=$(curl -v -u "$USERNAME" "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$REPO:pull,push" | jq '.token' | cut -d\" -f2)
# Push layers
# Step 1 - Get upload location
LOCATION=$(curl -i https://registry.hub.docker.com/v2/$REPO/blobs/uploads/ -H "Authorization: Bearer $TOKEN" -d '' | grep location | cut -d" " -f2 | tr -d '\r')
LOCATION=$(curl -i $LOCATION -X PATCH -H "Content-Length: $LAYERSIZE" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/octet-stream" --data-binary @hello-cloud.tar.gz | grep location | cut -d" " -f2 | tr -d '\r')
curl "$LOCATION&digest=sha256:$LAYERDIGEST" -X PUT -H "Authorization: Bearer $TOKEN" -v
# Push configuration.json
LOCATION=$(curl -i https://registry.hub.docker.com/v2/$REPO/blobs/uploads/ -H "Authorization: Bearer $TOKEN" -d '' | grep location | cut -d" " -f2 | tr -d '\r')
LOCATION=$(curl -i $LOCATION -X PATCH -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/octet-stream" --data-binary @configuration.json | grep location | cut -d" " -f2 | tr -d '\r')
curl "$LOCATION&digest=sha256:$CONFIGDIGEST" -X PUT -H "Authorization: Bearer $TOKEN" -v
# Push manifest
curl "https://registry.hub.docker.com/v2/$REPO/manifests/latest" -X PUT -H "Content-Type: application/vnd.oci.image.manifest.v1+json" -H "Authorization: Bearer $TOKEN" --data-binary @manifest.json -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment