使用 Jib-Core 将 Docker 镜像容器化到特定的本地目录

问题描述

我正在寻求帮助以使用 jib-core 创建本地 Docker 映像。

我知道如何将图像推送到 dockerhub 存储库,但现在我想将所有图像放入本地单个存储库。

到目前为止我所做的:

  • 通过阅读 jib-core 文档,成功创建了一个 image.tar 但无法构建它(缺少 dockerfile)

目前我是这样做的:

public void buildLocalImage(String appName) throws InvalidImageReferenceException,IOException,InterruptedException,ExecutionException,RegistryException,CacheDirectoryCreationException {
        jib.from("openjdk:15")
                .addLayer(Arrays.asList(Paths.get("jars/"+appName+".jar")),AbsoluteUnixPath.get("/"))
                .addLayer(Arrays.asList(Paths.get("jars/local-kube-api.jar")),AbsoluteUnixPath.get("/"))
                .addLayer(Arrays.asList(Paths.get("jars/script.sh")),AbsoluteUnixPath.get("/"))
                .setEntrypoint("sh","/script.sh")
                .containerize(
                        Containerizer.to(DockerDaemonImage.named("images/"+appName+"-images:TEST")).setofflineMode(true)
                );
    }

最终目标是在单个容器中运行 2 个 jar,当我使用传统方式(即通过添加凭据等推送到 dockerhub 存储库)时,这些 jar 可以成功运行。

上面的代码运行没有错误,但是我找不到后面的图片,不知道为什么。

我一定是哪里出错了。这是一个大学项目,如果您有任何我可以阅读的有关这些主题的文档,那将对我有很大帮助。

解决方法

public void buildLocalImage(String appName) throws InvalidImageReferenceException,IOException,InterruptedException,ExecutionException,RegistryException,CacheDirectoryCreationException {
        Jib.from("openjdk:15")
                .addLayer(Arrays.asList(Paths.get("jars/"+appName+".jar")),AbsoluteUnixPath.get("/"))
                .addLayer(Arrays.asList(Paths.get("jars/local-kube-api.jar")),AbsoluteUnixPath.get("/"))
                .addLayer(Arrays.asList(Paths.get("jars/script.sh")),AbsoluteUnixPath.get("/"))
                .setEntrypoint("sh","/script.sh")
                .containerize(
                        Containerizer.to(TarImage.at(Paths.get("images/"+appName+"-image:latest.tar"))
                                .named(appName+"-image:latest")));
    }

通过构建 .tar 镜像,然后通过 docker load < image_name 将其加载到 docker,您可以在容器中存储和运行多个本地 docker 镜像