如何在 Spring Boot 中使用 bootBuildImage 和 packeto 指定替代主类

问题描述 投票:0回答:1

在gradle中调用spring boot插件bootBuildImage任务时,使用packeto创建docker镜像。它启动 springBoot 插件中指定的主类。您可以在下面找到 build.gradle 文件的摘录。

springBoot {
    mainClass = 'MyMainApp'
}


bootBuildImage {
    imageName = "$docker_repo/${project.name}"
}

当调用

docker run
时,docker将运行一个从
MyMainApp
开始的容器。

但是,我想使用相同的 docker 映像运行另一个主类。我尝试了以下方法:

  • 指定
    -Dloader.main=MyOtherApp
    作为
    docker run
  • 中的 cmd
  • -Dloader.main=MyOtherApp
    环境变量中指定
    JAVA_TOOL_OPTIONS
  • 指定
    LOADER_MAIN=MyOtherApp
    作为环境变量

这些选项都没有启动

MyOtherApp

spring-boot paketo
1个回答
1
投票

Buildpacks 创建的映像提供了一些用于启动应用程序的有用工具。虽然这很好,但覆盖默认启动命令并不像仅向

docker run
指定新命令那么简单。

Buildpacks 提供的用于在映像中启动各种进程的所有工具都在文档中进行了描述

我在这里猜测了一下,但听起来您想运行自己的自定义进程(不是构建包检测到的进程),所以请尝试

这里的这个

您甚至可以覆盖 buildpack 定义的流程类型:

docker run --rm --entrypoint launcher -it multi-process-app bash docker run --rm --entrypoint launcher -it multi-process-app echo hello "$WORLD" # $WORLD is evaluated on the host machine docker run --rm --entrypoint launcher -it multi-process-app echo hello '$WORLD' # $WORLD is evaluated in the container after profile scripts are sourced

Java 应该在路径上,这样你就可以运行

java -Dloader.main=MyOtherApp org.springframework.boot.loader.launch.PropertiesLauncher

https://www.baeldung.com/spring-boot-main-class#using-cli


或者,您可以更改应用程序以默认使用 PropetiesLoader 并重建图像。 buildpack 只是从 MANIFEST.MF 文件中提取启动命令的启动器。不过,您需要使用 PropertiesLauncher,因为它支持

loader.main

。请参阅
https://stackoverflow.com/a/66367534/1585136

© www.soinside.com 2019 - 2024. All rights reserved.