Xavier Rodrigues
Xavier Rodrigues

Reputation: 11

Push image to docker registry using Java Http Client

This is the error I am getting on the Docker registry server:

10.41.59.51 - - [31/Jan/2024:15:25:34 +0000] "POST /v2/hello-world/blobs/uploads/ HTTP/1.1" 202 0 "" "Java-http-client/17.0.9"
time="2024-01-31T15:25:34.399380737Z" level=info msg="response completed" go.version=go1.20.8 http.request.contenttype="application/octet-stream" http.request.host="10.61.12.120:5000" http.request.id=87f0d0e7-ae16-4d93-a83e-4d64719bb8e2 http.request.method=POST http.request.remoteaddr="10.41.59.51:50385" http.request.uri="/v2/hello-world/blobs/uploads/57f40bbe-697c-4109-8f56-97cbc71ca717?_state=3B63IGFNngjKmbO962dooN31NtVHQw6NqD5xvwRCdiJ7Ik5hbWUiOiJoZWxsby13b3JsZCIsIlVVSUQiOiI1N2Y0MGJiZS02OTdjLTQxMDktOGY1Ni05N2NiYzcxY2E3MTciLCJPZmZzZXQiOjAsIlN0YXJ0ZWRBdCI6IjIwMjQtMDEtMzFUMTU6MjU6MzQuMzM3MjA2ODI1WiJ9" http.request.useragent="Java-http-client/17.0.9" http.response.duration=9.294838ms http.response.status=202 http.response.written=0 
10.41.59.51 - - [31/Jan/2024:15:25:34 +0000] "POST /v2/hello-world/blobs/uploads/57f40bbe-697c-4109-8f56-97cbc71ca717?_state=3B63IGFNngjKmbO962dooN31NtVHQw6NqD5xvwRCdiJ7Ik5hbWUiOiJoZWxsby13b3JsZCIsIlVVSUQiOiI1N2Y0MGJiZS02OTdjLTQxMDktOGY1Ni05N2NiYzcxY2E3MTciLCJPZmZzZXQiOjAsIlN0YXJ0ZWRBdCI6IjIwMjQtMDEtMzFUMTU6MjU6MzQuMzM3MjA2ODI1WiJ9 HTTP/1.1" 202 0 "" "Java-http-client/17.0.9"
time="2024-01-31T15:25:34.57232549Z" level=error msg="response completed with error" err.code="manifest invalid" err.detail="unexpected end of JSON input" err.message="manifest invalid" go.version=go1.20.8 http.request.contenttype="application/vnd.docker.distribution.manifest.v2+json" http.request.host="10.61.12.120:5000" http.request.id=5a469cf7-1ac7-439f-90b0-b77c554a32ec http.request.method=PUT http.request.remoteaddr="10.41.59.51:50386" http.request.uri="/v2/hello-world/manifests/latest" http.request.useragent="Java-http-client/17.0.9" http.response.contenttype="application/json; charset=utf-8" http.response.duration=1.768886ms http.response.status=400 http.response.written=92 vars.name=hello-world vars.reference=latest 

Here is my java code:

 private static String initiateUpload(String registryUrl, String imageName, String authHeaderValue) throws IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(registryUrl + "/v2/" + imageName + "/blobs/uploads/"))
                .header("Authorization", "Basic " + authHeaderValue)
                .POST(HttpRequest.BodyPublishers.noBody())
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        log.info(response.headers().firstValue("Location").toString());
        return response.headers().firstValue("Location").orElse(null);
    }

    private static void uploadImageLayers(String uploadUrl, String tarFilePath) throws IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uploadUrl))
                .header("Content-Type", "application/octet-stream")
                .POST(HttpRequest.BodyPublishers.ofFile(new File(tarFilePath).toPath()))
                .build();

        client.send(request, HttpResponse.BodyHandlers.discarding());
    }

    private static void finalizeUpload(String registryUrl, String imageName, String authHeaderValue) throws IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(registryUrl + "/v2/" + imageName + "/manifests/latest"))
                .header("Authorization", "Basic " + authHeaderValue)
                .header("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
                .PUT(HttpRequest.BodyPublishers.noBody())
                .build();

        client.send(request, HttpResponse.BodyHandlers.discarding());
    }

I am generating the tar file using the following docker save command

docker save -o hello_image.tar hello-world:latest

Here is the POST request from insomonia which I'm using for testing: Insomnia POST request

I have tried the following content types in my requests:

  1. application/vnd.docker.container.image.v1+json
  2. application/vnd.docker.image.rootfs.diff.tar.gzip

Upvotes: 1

Views: 86

Answers (0)

Related Questions