如何在使用spring-boot:build-image构建镜像时指定--network=host参数
spring-boot:build-image Build Phase Hey there, I totally get what you're trying to do—switching from direct docker build to Spring Boot's build-image plugin but needing that --network host setting for the build stage. Here's how you can make it work:
Option 1: Pass via Command Line
You can directly add the network configuration as a system property when running the Maven command. The property you need is spring-boot.build-image.builder.network:
mvn spring-boot:build-image -Dspring-boot.build-image.imageName=foo/bar -Dspring-boot.build-image.builder.network=host
This tells the underlying Cloud Native Buildpacks builder to use the host network during the build process, just like your old docker build --network host command did.
Option 2: Configure in pom.xml (Permanent Setup)
If you don't want to type the property every time, you can hardcode it in your Maven project's pom.xml by configuring the spring-boot-maven-plugin:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <name>foo/bar</name> </image> <builder> <network>host</network> </builder> </configuration> </plugin> </plugins> </build>
With this setup, you can just run mvn spring-boot:build-image and the network mode will automatically be set to host during the build.
Quick Note
This configuration applies only to the build phase (when the builder is pulling dependencies, running build steps, etc.)—it doesn't affect how the final image runs. Also, make sure you're using Spring Boot 2.4 or newer, since this property was introduced in that version.
内容的提问来源于stack exchange,提问作者Николай Беженар

