Skip to content

Commit

Permalink
fix: respects network configuration for buildx (#1850)
Browse files Browse the repository at this point in the history
* fix: respects network configuration for buildx

* docs: updates id of pull request

* chore: fixes Issue found by SonarQube
  • Loading branch information
michaelmejaeger authored Jan 10, 2025
1 parent dd58908 commit 94df38e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# ChangeLog

* **0.46-SNAPSHOT**:
- Docker-compose healthcheck configuration support ([1825](https://github.com/fabric8io/docker-maven-plugin/pull/1825))
- Docker container wait timeout default value made configurable using startContainerWaitTimeout configuration option ([1825](https://github.com/fabric8io/docker-maven-plugin/pull/1825))
- Respect `network` configuration in POM and with property `docker.build.network` or system property `docker.network.mode` in docker buildx [1850](https://github.com/fabric8io/docker-maven-plugin/pull/1850))

* **0.45.1 (2024-09-29)**:
- Make copy docker-buildx binary to temporary config directory work on windows too ([1819](https://github.com/fabric8io/docker-maven-plugin/pull/1819))
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/fabric8/maven/docker/config/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ private static void verifyImageNames(List<ImageConfiguration> ret) {
}
}

public static String getNetwork(ImageConfiguration imageConfig) {
String network = imageConfig.getBuildConfiguration().getNetwork();
if (network == null) {
network = System.getProperty("docker.network.mode");
}
if (network == null) {
network = System.getProperty("docker.build.network");
}
if(network!=null && network.isEmpty()) {
network = null;
}
return network;
}

public static boolean isNoCache(ImageConfiguration imageConfig) {
String noCache = System.getProperty("docker.noCache");
if (noCache == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ protected void buildX(List<String> buildX, String builderName, BuildDirs buildDi
cmdLine.add("--no-cache");
}

String networkMode = ConfigHelper.getNetwork(imageConfig);
if (networkMode!=null) {
cmdLine.add("--network="+networkMode);
}

BuildXConfiguration buildXConfiguration = buildConfiguration.getBuildX();
AttestationConfiguration attestations = buildXConfiguration.getAttestations();
if (attestations != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ void testBuildNativePlatformWithForeign() throws Exception {
verifyBuildXPlatforms(NATIVE);
}

@Test
void testNetworkIsPropagatedToBuildx() throws Exception {

//Given
buildConfigUsingBuildx(temporaryFolder, (buildX, buildImage) -> buildImage.network("host"));

// When
buildx.build(projectPaths, imageConfig, configuredRegistry, authConfigList, buildArchive);

//Then
verifyBuildXArgumentPresentInExec("--network=host");
}

@Test
void testNoCacheIsPropagatedToBuildx() throws Exception {

Expand Down

0 comments on commit 94df38e

Please sign in to comment.