prune998 / docker-registry-cleaner

a Go application to remove images from a remote Docker Registry
GNU General Public License v3.0
1 stars 0 forks source link

How to delete from Google #1

Open idontusenumbers opened 5 years ago

idontusenumbers commented 5 years ago

I stumbled upon this repo while trying to cleanup some docker images in google cloud repository; not sure if you're still maintaining this but I had luck with the following code:

private void deleteImage(String image, String tag) {
        try {
            log.info(MessageFormat.format("Deleting remote image: {0}:{1}", image, tag));

            String repositoryUrl = "https://gcr.io/v2/<projectid>";

            HttpGet manifestsRequest = new HttpGet(MessageFormat.format("{0}/{1}/manifests/{2}", repositoryUrl, image, tag));
            manifestsRequest.addHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json");

            String reference;
            try (CloseableHttpResponse manifestsResponse = client.execute(addBearerHeader(manifestsRequest))) {
                if (manifestsResponse.getStatusLine().getStatusCode() >= 400)
                    throw new RuntimeException("Unexpected response from container registry for manifest: " + manifestsResponse);
                reference = manifestsResponse.getFirstHeader("Docker-Content-Digest").getValue();
            }

            HttpDelete deleteTag = new HttpDelete(MessageFormat.format("{0}/{1}/manifests/{2}", repositoryUrl, image, tag));
            try (CloseableHttpResponse deleteTagResponse = client.execute(addBearerHeader(deleteTag))) {
                if (deleteTagResponse.getStatusLine().getStatusCode() >= 400)
                    throw new RuntimeException("Unexpected response from container registry for tag delete: " + deleteTagResponse);

            }
            HttpDelete deleteImage = new HttpDelete(MessageFormat.format("{0}/{1}/manifests/{2}", repositoryUrl, image, reference));
            try (CloseableHttpResponse deleteImageResponse = client.execute(addBearerHeader(deleteImage))) {
                if (deleteImageResponse.getStatusLine().getStatusCode() >= 400)
                    throw new RuntimeException("Unexpected response from container registry for image delete: " + deleteImageResponse);
            }
        } catch (Exception e) {
            throw new RuntimeException(MessageFormat.format("Could not remove image during cleanup {0}:{1}", image, tag), e);
        }
    }
akshatkarodiya commented 2 years ago

@prune998 I have faced the same issue, found out that i am using the wrong digest , you have to use the digest you received from header "Docker-Content-Digest" , then first delete the tags associated with image and then the image would be delete

Following code works fine

Python Function

def delete_image(service_name,token,tag):
    # manifest request 
    BASE_URL = "https://gcr.io/v2/";

    manifest_url = BASE_URL+'/'+service_name+'/'+'manifests/'+tag

    headers = {'Authorization': 'Bearer ' + token, "Accept": "application/vnd.docker.distribution.manifest.v2+json" }

    manifest_get_response = requests.get(manifest_url, headers=headers)

    if(manifest_get_response.status_code>=400):
        return "Not deleted error occured with getting manifest"

    # delete tag 

    del_tag_url = BASE_URL+PROJECT_NAME+'/'+service_name+'/'+'manifests/'+tag

    tag_delete_response = requests.delete(del_tag, headers=headers)

    if(tag_response.status_code>=400):
        return "Not deleted error occured with tag deletion"

    # delete image 

    manifest_digest = r.headers['Docker-Content-Digest']

    del_image_url = BASE_URL+PROJECT_NAME+'/'+service_name+'/'+'manifests/'+manifest_digest

    image_delete_response = requests.delete(del_url, headers=headers)

    if(image_delete_response.status_code>=400):
        return "Not deleted error occured with image deletion"

    return "Deleted"