Skip to content

Commit a961200

Browse files
authored
Handle network aliases without a custom network gracefully (docker-java#1942)
Fixes docker-java#1940
1 parent 8253a99 commit a961200

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/model/HostConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ public HostConfig withMemorySwappiness(Long memorySwappiness) {
848848
* <li>'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system
849849
* services such as D-bus and is therefore considered insecure.</li>
850850
* </ul>
851+
* Any other value is interpreted as a custom network's name for this container to connect to.
851852
*/
852853
public HostConfig withNetworkMode(String networkMode) {
853854
this.networkMode = networkMode;

docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public CreateContainerResponse exec() throws NotFoundException, ConflictExceptio
600600
containerNetwork.withAliases(aliases);
601601
}
602602

603-
if (containerNetwork != null) {
603+
if (containerNetwork != null && hostConfig.getNetworkMode() != null) {
604604
networkingConfig = new NetworkingConfig()
605605
.withEndpointsConfig(singletonMap(hostConfig.getNetworkMode(), containerNetwork));
606606
}

docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.github.dockerjava.api.model.Link;
2424
import com.github.dockerjava.api.model.LogConfig;
2525
import com.github.dockerjava.api.model.Network;
26+
import com.github.dockerjava.api.model.PortBinding;
2627
import com.github.dockerjava.api.model.Ports;
2728
import com.github.dockerjava.api.model.Ports.Binding;
2829
import com.github.dockerjava.api.model.RestartPolicy;
@@ -1125,4 +1126,15 @@ public void shouldNotEncodeAuth() {
11251126

11261127
assertThat(jsonNode.get("authConfig"), nullValue());
11271128
}
1129+
1130+
@Test
1131+
public void shouldHandleANetworkAliasWithoutACustomNetworkGracefully() {
1132+
// Should not throw
1133+
dockerRule.getClient()
1134+
.createContainerCmd(DEFAULT_IMAGE)
1135+
.withAliases("hello-world")
1136+
.withHostConfig(newHostConfig())
1137+
.withCmd("sleep", "9999")
1138+
.exec();
1139+
}
11281140
}

0 commit comments

Comments
 (0)