我一直在尝试使用构建包生成 Spring Boot 映像。
我使用 https://start.spring.io/ 和 此配置 生成了项目。
我只添加了一个控制器来从服务器获取一些响应:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
并配置 gradle 来使用
paketobuildpacks/builder-jammy-base:latest
tasks.named("bootBuildImage") {
builder = "paketobuildpacks/builder-jammy-base:latest"
}
如果我使用
./gradlew bootBuildImage
构建此图像,一切都会很好。
但是如果我尝试使用
pack
,当尝试访问 /
时,我会从 tomcat 收到 404。
$ pack build springboot-demo --builder=paketobuildpacks/builder-jammy-base:latest
$ docker run -p 8080:8080 springboot-demo
Calculating JVM memory based on 3593560K available memory
For more information on this calculation, see https://paketo.io/docs/reference/java-reference/#memory-calculator
Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx2986909K -XX:MaxMetaspaceSize=94650K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 3593560K, Thread Count: 250, Loaded Class Count: 14297, Headroom: 0%)
Enabling Java Native Memory Tracking
Adding 137 container CA certificates to JVM truststore
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -XX:+ExitOnOutOfMemoryError -XX:MaxDirectMemorySize=10M -Xmx2986909K -XX:MaxMetaspaceSize=94650K -XX:ReservedCodeCacheSize=240M -Xss1M -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics
[CONTAINER] org.apache.coyote.http11.Http11NioProtocol INFO Initializing ProtocolHandler ["http-nio-8080"]
[CONTAINER] org.apache.catalina.startup.Catalina INFO Server initialization in [570] milliseconds
[CONTAINER] org.apache.catalina.core.StandardService INFO Starting service [Catalina]
[CONTAINER] org.apache.catalina.core.StandardEngine INFO Starting Servlet engine: [Apache Tomcat/9.0.89]
[CONTAINER] org.apache.catalina.startup.HostConfig INFO Deploying web application directory [/workspace/demo-0.0.1-SNAPSHOT]
[CONTAINER] org.apache.tomcat.util.descriptor.web.WebXml WARNING Unknown version string [5.0]. Default version will be used.
[CONTAINER] org.apache.jasper.servlet.TldScanner INFO At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[CONTAINER] org.apache.catalina.startup.HostConfig INFO Deployment of web application directory [/workspace/demo-0.0.1-SNAPSHOT] has finished in [1,393] ms
[CONTAINER] org.apache.catalina.startup.HostConfig INFO Deploying web application directory [/workspace/demo-0.0.1-SNAPSHOT-plain]
[CONTAINER] org.apache.tomcat.util.descriptor.web.WebXml WARNING Unknown version string [5.0]. Default version will be used.
[CONTAINER] org.apache.jasper.servlet.TldScanner INFO At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[CONTAINER] org.apache.catalina.startup.HostConfig INFO Deployment of web application directory [/workspace/demo-0.0.1-SNAPSHOT-plain] has finished in [837] ms
[CONTAINER] org.apache.coyote.http11.Http11NioProtocol INFO Starting ProtocolHandler ["http-nio-8080"]
[CONTAINER] org.apache.catalina.startup.Catalina INFO Server startup in [2276] milliseconds
我可以看到参与构建的构建包是不同的: 对于
bootBuildImage
:
[creator] paketo-buildpacks/ca-certificates 3.7.0
[creator] paketo-buildpacks/bellsoft-liberica 10.7.2
[creator] paketo-buildpacks/syft 1.46.0
[creator] paketo-buildpacks/executable-jar 6.9.0
[creator] paketo-buildpacks/dist-zip 5.7.0
[creator] paketo-buildpacks/spring-boot 5.29.1
对于
pack build
:
[detector] paketo-buildpacks/ca-certificates 3.7.0
[detector] paketo-buildpacks/bellsoft-liberica 10.7.2
[detector] paketo-buildpacks/syft 1.46.0
[detector] paketo-buildpacks/gradle 7.10.0
[detector] paketo-buildpacks/executable-jar 6.9.0
[detector] paketo-buildpacks/apache-tomcat 8.0.0
[detector] paketo-buildpacks/apache-tomee 1.9.0
[detector] paketo-buildpacks/liberty 4.1.0
[detector] paketo-buildpacks/dist-zip 5.7.0
[detector] paketo-buildpacks/spring-boot 5.29.1
如何让
pack build
的行为与 gradle 命令相同?
我不完全明白为什么,但禁用战争生成可以使图像发挥作用。
war {
enabled = false
}
将其添加到
build.gradle
解决了问题。
跑步:
pack build springboot-demo --builder=paketobuildpacks/builder-jammy-base:latest
生成工作图像。