1

I use Java to build a Docker image. The Dockerfile and code are ready. I can build an image successfully through CLI, such as:

docker build -t imgetest:1.0 .

My Dockerfile is:

FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD [ "node", "server.js" ]

It is a very simple Dockerfile, only for testing.

But I can't build through Java API. My maven dependency and Java code are:

<dependency>
    <groupId>com.github.docker-java</groupId>
    <artifactId>docker-java</artifactId>
    <version>3.2.1</version>
</dependency>
public boolean createImages(String repository, String dockerfilePath)
{
    try {
        // Loading from a source
        final File imageFile = new File(dockerfilePath);
        try (InputStream imagePayload = new BufferedInputStream(new FileInputStream(imageFile))) {
            dockerClient.createImageCmd(repository, imagePayload).exec();
            dockerClient.close();
            logger.info("Build image successfully.");
            return Boolean.TRUE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            dockerClient.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return Boolean.FALSE;
}

My docker environment is: Docker env My client is:

    public static DockerClient getClient()
    {
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build();

        DockerClient client = DockerClientBuilder
                .getInstance(config)
                .build();
        return client;
    }

The error message is:

20:46:18.553 [main] DEBUG com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory - 1 * Client response received on thread main
1 < 500
1 < Api-Version: 1.40
1 < Content-Length: 71
1 < Content-Type: application/json
1 < Date: Sat, 06 Jun 2020 12:46:18 GMT
1 < Docker-Experimental: false
1 < Ostype: linux
1 < Server: Docker/19.03.8 (linux)
{"message":"Error processing tar file(exit status 1): unexpected EOF"}


20:46:18.566 [main] DEBUG com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory$1 - Connection [id: 0][route: {}->unix://localhost:80] can be kept alive indefinitely
20:46:18.566 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: set socket timeout to 0
20:46:18.567 [main] DEBUG com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory$1 - Connection released: [id: 0][route: {}->unix://localhost:80][total available: 1; route allocated: 1 of 2; total allocated: 1 of 20]
com.github.dockerjava.api.exception.InternalServerErrorException: Error processing tar file(exit status 1): unexpected EOF
    at com.github.dockerjava.jaxrs.filter.ResponseStatusExceptionFilter.filter(ResponseStatusExceptionFilter.java:72)
    at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:131)
    at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:119)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:147)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:259)
    at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:736)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:735)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:421)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:327)
    at com.github.dockerjava.jaxrs.JerseyInvocationBuilder.post(JerseyInvocationBuilder.java:145)
    at com.github.dockerjava.core.exec.CreateImageCmdExec.execute(CreateImageCmdExec.java:33)
    at com.github.dockerjava.core.exec.CreateImageCmdExec.execute(CreateImageCmdExec.java:13)
    at com.github.dockerjava.core.exec.AbstrSyncDockerCmdExec.exec(AbstrSyncDockerCmdExec.java:21)
    at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:35)

Who can help me giving some ideas? Thanks ahead.

3
  • can you also paste the dockerfile content here? Commented Jun 6, 2020 at 13:29
  • Yes. already added. Commented Jun 6, 2020 at 13:36
  • I have never used docker-java, but seems like it supports multiple transports, have you tried with other transports, say okhttp or netty, do you see same error? See the note from their official doc: "Since Apache HttpClient 5-based transport is available now, there is no reason to keep Jersey and it will eventually be removed." github.com/docker-java/docker-java/blob/master/docs/… Commented Jun 6, 2020 at 13:44

1 Answer 1

0

Here is a solution for this issue. It works for me.

BuildImageResultCallback callback = new BuildImageResultCallback() {
                    @Override
                    public void onNext(BuildResponseItem item) {
                        System.out.println("" + item);
                        super.onNext(item);
                        }
                    };
File baseDir = new File(dockerfilePath);
String imageId = dockerClient.buildImageCmd(baseDir).exec(callback).awaitImageId();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.