Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit cfa515f

Browse files
committed
Reject multiple networks on container creation
Signed-off-by: Alessandro Boch <aboch@docker.com>
1 parent 15cc67b commit cfa515f

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

daemon/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig) (types
2626
return types.ContainerCreateResponse{Warnings: warnings}, err
2727
}
2828

29+
err = daemon.verifyNetworkingConfig(params.NetworkingConfig)
30+
if err != nil {
31+
return types.ContainerCreateResponse{}, err
32+
}
33+
2934
if params.HostConfig == nil {
3035
params.HostConfig = &containertypes.HostConfig{}
3136
}

daemon/daemon.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
containertypes "github.com/docker/engine-api/types/container"
3232
eventtypes "github.com/docker/engine-api/types/events"
3333
"github.com/docker/engine-api/types/filters"
34+
networktypes "github.com/docker/engine-api/types/network"
3435
registrytypes "github.com/docker/engine-api/types/registry"
3536
"github.com/docker/engine-api/types/strslice"
3637
// register graph drivers
@@ -1416,6 +1417,18 @@ func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostCon
14161417
return verifyPlatformContainerSettings(daemon, hostConfig, config)
14171418
}
14181419

1420+
// Checks if the client set configurations for more than one network while creating a container
1421+
func (daemon *Daemon) verifyNetworkingConfig(nwConfig *networktypes.NetworkingConfig) error {
1422+
if nwConfig == nil || len(nwConfig.EndpointsConfig) <= 1 {
1423+
return nil
1424+
}
1425+
l := make([]string, 0, len(nwConfig.EndpointsConfig))
1426+
for k := range nwConfig.EndpointsConfig {
1427+
l = append(l, k)
1428+
}
1429+
return derr.ErrorCodeMultipleNetworkConnect.WithArgs(fmt.Sprintf("%v", l))
1430+
}
1431+
14191432
func configureVolumes(config *Config, rootUID, rootGID int) (*store.VolumeStore, error) {
14201433
volumesDriver, err := local.New(config.Root, rootUID, rootGID)
14211434
if err != nil {

errors/daemon.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,4 +966,13 @@ var (
966966
Description: "Engine's predefined networks cannot be deleted",
967967
HTTPStatusCode: http.StatusForbidden,
968968
})
969+
970+
// ErrorCodeMultipleNetworkConnect is generated when more than one network is passed
971+
// when creating a container
972+
ErrorCodeMultipleNetworkConnect = errcode.Register(errGroup, errcode.ErrorDescriptor{
973+
Value: "CANNOT_CONNECT_TO_MULTIPLE_NETWORKS",
974+
Message: "Container cannot be connected to %s",
975+
Description: "A container can only be connected to one network at the time",
976+
HTTPStatusCode: http.StatusBadRequest,
977+
})
969978
)

integration-cli/docker_api_containers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/docker/pkg/stringid"
2121
"github.com/docker/engine-api/types"
2222
containertypes "github.com/docker/engine-api/types/container"
23+
networktypes "github.com/docker/engine-api/types/network"
2324
"github.com/go-check/check"
2425
)
2526

@@ -604,6 +605,29 @@ func (s *DockerSuite) TestContainerApiCreateEmptyConfig(c *check.C) {
604605
c.Assert(string(b), checker.Equals, expected)
605606
}
606607

608+
func (s *DockerSuite) TestContainerApiCreateMultipleNetworksConfig(c *check.C) {
609+
// Container creation must fail if client specified configurations for more than one network
610+
config := map[string]interface{}{
611+
"Image": "busybox",
612+
"NetworkingConfig": networktypes.NetworkingConfig{
613+
EndpointsConfig: map[string]*networktypes.EndpointSettings{
614+
"net1": {},
615+
"net2": {},
616+
"net3": {},
617+
},
618+
},
619+
}
620+
621+
status, b, err := sockRequest("POST", "/containers/create", config)
622+
c.Assert(err, checker.IsNil)
623+
c.Assert(status, checker.Equals, http.StatusBadRequest)
624+
// network name order in error message is not deterministic
625+
c.Assert(string(b), checker.Contains, "Container cannot be connected to [")
626+
c.Assert(string(b), checker.Contains, "net1")
627+
c.Assert(string(b), checker.Contains, "net2")
628+
c.Assert(string(b), checker.Contains, "net3")
629+
}
630+
607631
func (s *DockerSuite) TestContainerApiCreateWithHostName(c *check.C) {
608632
testRequires(c, DaemonIsLinux)
609633
hostName := "test-host"

0 commit comments

Comments
 (0)