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

@@ -169,6 +169,14 @@ Where `<options>` can contain:
| `--publishImage`
| Whether to publish the generated image to a Docker registry.
| `false`
| `network`
| `--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 JavaPlugin's `targetCompatibility` property.

View File

@@ -58,6 +58,7 @@ import org.springframework.util.StringUtils;
*
* @author Andy Wilkinson
* @author Scott Frederick
* @author Jeroen Meijer
* @since 2.3.0
*/
public class BootBuildImage extends DefaultTask {
@@ -92,6 +93,8 @@ public class BootBuildImage extends DefaultTask {
private final ListProperty<String> bindings;
private String network;
private final DockerSpec docker = new DockerSpec();
public BootBuildImage() {
@@ -376,6 +379,25 @@ public class BootBuildImage extends DefaultTask {
this.bindings.addAll(bindings);
}
/**
* Returns the network the build container will connect to.
* @return the network
*/
@Input
@Optional
public String getNetwork() {
return this.network;
}
/**
* Sets the network the build container will connect to.
* @param network the network
*/
@Option(option = "network", description = "Connect detect and build containers to network")
public void setNetwork(String network) {
this.network = network;
}
/**
* Returns the Docker configuration the builder will use.
* @return docker configuration.
@@ -438,6 +460,7 @@ public class BootBuildImage extends DefaultTask {
request = customizePublish(request);
request = customizeBuildpacks(request);
request = customizeBindings(request);
request = request.withNetwork(this.network);
return request;
}

View File

@@ -42,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Andy Wilkinson
* @author Scott Frederick
* @author Andrey Shlykov
* @author Jeroen Meijer
*/
class BootBuildImageTests {
@@ -278,4 +279,10 @@ class BootBuildImageTests {
.containsExactly(Binding.of("host-src:container-dest:ro"), Binding.of("volume-name:container-dest:rw"));
}
@Test
void whenNetworkIsConfiguredThenRequestHasNetwork() {
this.buildImage.setNetwork("test");
assertThat(this.buildImage.createRequest().getNetwork()).isEqualTo("test");
}
}