在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
-Dloader.main=MyOtherApp
环境变量中指定 JAVA_TOOL_OPTIONS
LOADER_MAIN=MyOtherApp
作为环境变量这些选项都没有启动
MyOtherApp
。
Buildpacks 创建的映像提供了一些用于启动应用程序的有用工具。虽然这很好,但覆盖默认启动命令并不像仅向
docker run
指定新命令那么简单。
Buildpacks 提供的用于在映像中启动各种进程的所有工具都在文档中进行了描述。
我在这里猜测了一下,但听起来您想运行自己的自定义进程(不是构建包检测到的进程),所以请尝试这里的这个。
您甚至可以覆盖 buildpack 定义的流程类型:Java 应该在路径上,这样你就可以运行
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 -Dloader.main=MyOtherApp org.springframework.boot.loader.launch.PropertiesLauncher
。
https://www.baeldung.com/spring-boot-main-class#using-cli
loader.main
。请参阅https://stackoverflow.com/a/66367534/1585136。