-
Notifications
You must be signed in to change notification settings - Fork 1.1k
allow CreateContainer to specify NetworkConfig #2556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.github.dockerjava.api.model; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Networking configuration for a container. | ||
| */ | ||
| public class NetworkingConfig { | ||
|
|
||
| public NetworkingConfig() { | ||
| this.endpointsConfig = new HashMap<>(); | ||
| } | ||
| public NetworkingConfig(Map<String, ContainerNetwork> endpointsConfig) { | ||
| this.endpointsConfig = endpointsConfig; | ||
| } | ||
|
|
||
| @JsonProperty("EndpointsConfig") | ||
| private Map<String, ContainerNetwork> endpointsConfig; | ||
|
|
||
| @CheckForNull | ||
| public Map<String, ContainerNetwork> getEndpointsConfig() { | ||
| return endpointsConfig; | ||
| } | ||
|
|
||
| public NetworkingConfig withEndpointsConfig(Map<String, ContainerNetwork> endpointsConfig) { | ||
| this.endpointsConfig = endpointsConfig; | ||
| return this; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||||
| import com.github.dockerjava.api.model.ExposedPorts; | ||||||||||||||||||||||
| import com.github.dockerjava.api.model.HealthCheck; | ||||||||||||||||||||||
| import com.github.dockerjava.api.model.HostConfig; | ||||||||||||||||||||||
| import com.github.dockerjava.api.model.NetworkingConfig; | ||||||||||||||||||||||
| import com.github.dockerjava.api.model.Volume; | ||||||||||||||||||||||
| import com.github.dockerjava.api.model.Volumes; | ||||||||||||||||||||||
| import org.apache.commons.lang3.builder.EqualsBuilder; | ||||||||||||||||||||||
|
|
@@ -127,7 +128,7 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C | |||||||||||||||||||||
| private List<String> shell; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @JsonProperty("NetworkingConfig") | ||||||||||||||||||||||
| private NetworkingConfig networkingConfig; | ||||||||||||||||||||||
| private NetworkingConfig networkingConfig = new NetworkingConfig(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private String ipv4Address = null; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -544,6 +545,18 @@ public CreateContainerCmd withIpv6Address(String ipv6Address) { | |||||||||||||||||||||
| return this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Override | ||||||||||||||||||||||
| public NetworkingConfig getNetworkingConfig() { | ||||||||||||||||||||||
| return networkingConfig; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Override | ||||||||||||||||||||||
| public CreateContainerCmd withNetworkingConfig(NetworkingConfig networkingConfig) { | ||||||||||||||||||||||
| Objects.requireNonNull(networkingConfig, "no networkingConfig was specified"); | ||||||||||||||||||||||
| this.networkingConfig = networkingConfig; | ||||||||||||||||||||||
| return this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @CheckForNull | ||||||||||||||||||||||
| public List<String> getOnBuild() { | ||||||||||||||||||||||
| return onBuild; | ||||||||||||||||||||||
|
|
@@ -601,8 +614,13 @@ public CreateContainerResponse exec() throws NotFoundException, ConflictExceptio | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (containerNetwork != null && hostConfig.getNetworkMode() != null) { | ||||||||||||||||||||||
| networkingConfig = new NetworkingConfig() | ||||||||||||||||||||||
| .withEndpointsConfig(singletonMap(hostConfig.getNetworkMode(), containerNetwork)); | ||||||||||||||||||||||
| // If the user explicitly sets networkingConfig or endpointConfig to null, its reasonable to not overwrite it | ||||||||||||||||||||||
|
||||||||||||||||||||||
| // If the user explicitly sets networkingConfig or endpointConfig to null, its reasonable to not overwrite it | |
| // If the user explicitly sets networkingConfig or endpointConfig to null, it's reasonable to not overwrite it |
Copilot
AI
Mar 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change adds a new public API surface (withNetworkingConfig/getNetworkingConfig) and changes create-container networking behavior (merging endpoint configs). There are existing integration tests for container networking in CreateContainerCmdIT, but none that assert attaching to multiple networks at creation via NetworkingConfig. Adding a test that creates two networks and verifies the container is attached to both would help prevent regressions.
| // If the user explicitly sets networkingConfig or endpointConfig to null, its reasonable to not overwrite it | |
| if (networkingConfig != null | |
| && networkingConfig.getEndpointsConfig() != null | |
| && !networkingConfig.getEndpointsConfig().containsKey(hostConfig.getNetworkMode()) | |
| ) { | |
| // If the user already provided endpointsConfig, merge without overwriting existing entries. | |
| if (networkingConfig == null || networkingConfig.getEndpointsConfig() == null) { | |
| networkingConfig = new NetworkingConfig() | |
| .withEndpointsConfig(singletonMap(hostConfig.getNetworkMode(), containerNetwork)); | |
| } else if (!networkingConfig.getEndpointsConfig().containsKey(hostConfig.getNetworkMode())) { |
Copilot
AI
Mar 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
singletonMap is no longer used in this class after the networkingConfig logic change. Because the build runs Checkstyle with failOnViolation=true, this unused static import will likely fail CI; remove the now-unused import static java.util.Collections.singletonMap;.
Copilot
AI
Mar 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
networkingConfig.getEndpointsConfig().put(...) mutates the caller-provided map. If the user supplied an immutable map (e.g., Map.of(...)/Collections.unmodifiableMap(...)), this will throw UnsupportedOperationException at runtime. Consider copying to a mutable map (and setting it back via withEndpointsConfig) before adding/merging the endpoint, and also handle the null endpointsConfig case by creating a new map when you need to add the default network endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NetworkingConfigas added won’t compile and will fail existing architecture/serialization tests: it usesHashMapwithout importing it, and it does not extendDockerObjectnor implementSerializable/serialVersionUID(seedocker-java-apiArchUnit test andModelsSerializableTestwhich enforce these conventions forcom.github.dockerjava.api.modelclasses). Update the class to follow the existing model conventions (extendDockerObject, implementSerializable, addserialVersionUID, and fix missing imports).