Add network option for image building

This commit adds configuration to the Maven and Gradle plugins to
allow specifying the network mode to be provided to the image
building goal and task.

See gh-27486
This commit is contained in:
Jeroen Meijer
2021-07-24 15:47:01 +02:00
committed by Scott Frederick
parent e737388f5c
commit 8e6d03b221
17 changed files with 195 additions and 15 deletions

View File

@@ -176,6 +176,14 @@ Where `<options>` can contain:
(`spring-boot.build-image.publish`)
| Whether to publish the generated image to a Docker registry.
| `false`
| `network` +
(`spring-boot.build-image.network`)
| The network the build container will connect to. The value supplied for this option will be passed
unvalidated as `HostConfig.NetworkMode` to the configuration which creates the build container,
see https://docs.docker.com/engine/api/v1.41/#operation/ContainerCreate[Docker's engine API].
Using this option is similar to running `docker build --network ...`.
|
|===
NOTE: The plugin detects the target Java compatibility of the project using the compiler's plugin configuration or the `maven.compiler.target` property.

View File

@@ -59,6 +59,7 @@ import org.springframework.util.StringUtils;
*
* @author Phillip Webb
* @author Scott Frederick
* @author Jeroen Meijer
* @since 2.3.0
*/
@Mojo(name = "build-image", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
@@ -153,6 +154,13 @@ public class BuildImageMojo extends AbstractPackagerMojo {
@Parameter(property = "spring-boot.build-image.publish", readonly = true)
Boolean publish;
/**
* Alias for {@link Image#network} to support configuration via command-line property.
* @since 2.6.0
*/
@Parameter(property = "spring-boot.build-image.network", readonly = true)
String network;
/**
* Docker configuration options.
* @since 2.4.0
@@ -248,6 +256,9 @@ public class BuildImageMojo extends AbstractPackagerMojo {
if (image.publish == null && this.publish != null) {
image.setPublish(this.publish);
}
if (image.network == null && this.network != null) {
image.setNetwork(this.network);
}
if (image.publish != null && image.publish && publishRegistryNotConfigured()) {
throw new MojoExecutionException("Publishing an image requires docker.publishRegistry to be configured");
}

View File

@@ -39,6 +39,7 @@ import org.springframework.util.StringUtils;
*
* @author Phillip Webb
* @author Scott Frederick
* @author Jeroen Meijer
* @since 2.3.0
*/
public class Image {
@@ -63,6 +64,8 @@ public class Image {
List<String> bindings;
String network;
/**
* The name of the created image.
* @return the image name
@@ -151,6 +154,18 @@ public class Image {
this.publish = publish;
}
/**
* Returns the network the build container will connect to.
* @return the network
*/
public String getNetwork() {
return this.network;
}
public void setNetwork(String network) {
this.network = network;
}
BuildRequest getBuildRequest(Artifact artifact, Function<Owner, TarArchive> applicationContent) {
return customize(BuildRequest.of(getOrDeduceName(artifact), applicationContent));
}
@@ -190,6 +205,7 @@ public class Image {
if (!CollectionUtils.isEmpty(this.bindings)) {
request = request.withBindings(this.bindings.stream().map(Binding::of).collect(Collectors.toList()));
}
request = request.withNetwork(this.network);
return request;
}

View File

@@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.entry;
*
* @author Phillip Webb
* @author Scott Frederick
* @author Jeroen Meijer
*/
class ImageTests {
@@ -70,6 +71,7 @@ class ImageTests {
assertThat(request.getPullPolicy()).isEqualTo(PullPolicy.ALWAYS);
assertThat(request.getBuildpacks()).isEmpty();
assertThat(request.getBindings()).isEmpty();
assertThat(request.getNetwork()).isNull();
}
@Test
@@ -146,6 +148,14 @@ class ImageTests {
Binding.of("volume-name:container-dest:rw"));
}
@Test
void getBuildRequestWhenNetworkUsesNetwork() {
Image image = new Image();
image.network = "test";
BuildRequest request = image.getBuildRequest(createArtifact(), mockApplicationContent());
assertThat(request.getNetwork()).isEqualTo("test");
}
private Artifact createArtifact() {
return new DefaultArtifact("com.example", "my-app", VersionRange.createFromVersion("0.0.1-SNAPSHOT"), "compile",
"jar", null, new DefaultArtifactHandler());