gpt4 book ai didi

java - 无法使用Java创建docker镜像。错误消息是 “…: Error processing tar file(exit status 1): unexpected EOF”

转载 作者:行者123 更新时间:2023-12-02 19:48:30 27 4
gpt4 key购买 nike

我使用Java构建Docker镜像。 Dockerfile和代码已准备就绪。我可以通过CLI成功构建镜像,例如:

docker build -t imgetest:1.0 .



我的Dockerfile是:
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD [ "node", "server.js" ]

这是一个非常简单的Dockerfile,仅用于测试。

但是我无法通过Java API构建。我的Maven依赖项和Java代码是:

<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;
}

我的 docker 环境是:
Docker env
我的客户是:
    public static DockerClient getClient()
{
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build();

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

错误消息是:
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)

谁能帮助我提出一些想法?谢谢你

最佳答案

这是此问题的解决方案。这个对我有用。

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();

关于java - 无法使用Java创建docker镜像。错误消息是 “…: Error processing tar file(exit status 1): unexpected EOF”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62232204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com