Skip to content

allow CreateContainer to specify NetworkConfig#2556

Open
nbrugger-tgm wants to merge 1 commit intodocker-java:mainfrom
nbrugger-tgm:main
Open

allow CreateContainer to specify NetworkConfig#2556
nbrugger-tgm wants to merge 1 commit intodocker-java:mainfrom
nbrugger-tgm:main

Conversation

@nbrugger-tgm
Copy link

@nbrugger-tgm nbrugger-tgm commented Dec 8, 2025

Closes: #2555

@nbrugger-tgm nbrugger-tgm requested a review from a team as a code owner December 8, 2025 22:49
@eddumelendez eddumelendez changed the title feat: allow CreateContainer to specify NetworkConfig allow CreateContainer to specify NetworkConfig Dec 9, 2025
@eddumelendez eddumelendez requested a review from Copilot March 21, 2026 00:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for specifying Docker’s NetworkingConfig when creating a container, enabling attachment to multiple networks at creation time (per #2555).

Changes:

  • Introduces a new public API model com.github.dockerjava.api.model.NetworkingConfig.
  • Exposes getNetworkingConfig() / withNetworkingConfig(...) on CreateContainerCmd.
  • Updates CreateContainerCmdImpl#exec() to merge endpoint settings instead of overwriting them.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java Adds networkingConfig accessors and changes exec() to merge endpoint configs rather than replace them.
docker-java-api/src/main/java/com/github/dockerjava/api/model/NetworkingConfig.java Adds a new API model for Docker NetworkingConfig.
docker-java-api/src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java Exposes networkingConfig getter/setter on the public command interface.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: "its reasonable" should be "it's reasonable".

Suggested change
// 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 uses AI. Check for mistakes.
Comment on lines 616 to +623
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 (networkingConfig != null
&& networkingConfig.getEndpointsConfig() != null
&& !networkingConfig.getEndpointsConfig().containsKey(hostConfig.getNetworkMode())
) {
networkingConfig.getEndpointsConfig().put(hostConfig.getNetworkMode(), containerNetwork);
}
Copy link

Copilot AI Mar 21, 2026

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 uses AI. Check for mistakes.
Comment on lines +617 to +621
// 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())
) {
Copy link

Copilot AI Mar 21, 2026

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.

Suggested change
// 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 uses AI. Check for mistakes.
Comment on lines +11 to +18
public class NetworkingConfig {

public NetworkingConfig() {
this.endpointsConfig = new HashMap<>();
}
public NetworkingConfig(Map<String, ContainerNetwork> endpointsConfig) {
this.endpointsConfig = endpointsConfig;
}
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NetworkingConfig as added won’t compile and will fail existing architecture/serialization tests: it uses HashMap without importing it, and it does not extend DockerObject nor implement Serializable/serialVersionUID (see docker-java-api ArchUnit test and ModelsSerializableTest which enforce these conventions for com.github.dockerjava.api.model classes). Update the class to follow the existing model conventions (extend DockerObject, implement Serializable, add serialVersionUID, and fix missing imports).

Copilot uses AI. Check for mistakes.
Comment on lines 616 to +623
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 (networkingConfig != null
&& networkingConfig.getEndpointsConfig() != null
&& !networkingConfig.getEndpointsConfig().containsKey(hostConfig.getNetworkMode())
) {
networkingConfig.getEndpointsConfig().put(hostConfig.getNetworkMode(), containerNetwork);
}
Copy link

Copilot AI Mar 21, 2026

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating container doesn't allow for setting "NetworkingConfig"

2 participants