link feature in docker0 bridge by default provides short-id as a
container alias. With built-in SD feature, providing a container
short-id as a network alias will fill that gap.
Signed-off-by: Madhu Venugopal <madhu@docker.com>
... | ... |
@@ -12,6 +12,7 @@ import ( |
12 | 12 |
"github.com/docker/docker/container" |
13 | 13 |
"github.com/docker/docker/daemon/network" |
14 | 14 |
derr "github.com/docker/docker/errors" |
15 |
+ "github.com/docker/docker/pkg/stringid" |
|
15 | 16 |
"github.com/docker/docker/runconfig" |
16 | 17 |
containertypes "github.com/docker/engine-api/types/container" |
17 | 18 |
networktypes "github.com/docker/engine-api/types/network" |
... | ... |
@@ -485,6 +486,18 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrNa |
485 | 485 |
if endpointConfig != nil && len(endpointConfig.Aliases) > 0 { |
486 | 486 |
return nil, runconfig.ErrUnsupportedNetworkAndAlias |
487 | 487 |
} |
488 |
+ } else { |
|
489 |
+ addShortID := true |
|
490 |
+ shortID := stringid.TruncateID(container.ID) |
|
491 |
+ for _, alias := range endpointConfig.Aliases { |
|
492 |
+ if alias == shortID { |
|
493 |
+ addShortID = false |
|
494 |
+ break |
|
495 |
+ } |
|
496 |
+ } |
|
497 |
+ if addShortID { |
|
498 |
+ endpointConfig.Aliases = append(endpointConfig.Aliases, shortID) |
|
499 |
+ } |
|
488 | 500 |
} |
489 | 501 |
|
490 | 502 |
n, err := daemon.FindNetwork(idOrName) |
... | ... |
@@ -505,6 +518,9 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrNa |
505 | 505 |
} |
506 | 506 |
|
507 | 507 |
func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) { |
508 |
+ if endpointConfig == nil { |
|
509 |
+ endpointConfig = &networktypes.EndpointSettings{} |
|
510 |
+ } |
|
508 | 511 |
n, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, updateSettings) |
509 | 512 |
if err != nil { |
510 | 513 |
return err |
... | ... |
@@ -533,10 +549,7 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName |
533 | 533 |
} |
534 | 534 |
} |
535 | 535 |
}() |
536 |
- |
|
537 |
- if endpointConfig != nil { |
|
538 |
- container.NetworkSettings.Networks[n.Name()] = endpointConfig |
|
539 |
- } |
|
536 |
+ container.NetworkSettings.Networks[n.Name()] = endpointConfig |
|
540 | 537 |
|
541 | 538 |
if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil { |
542 | 539 |
return err |
... | ... |
@@ -101,6 +101,9 @@ func (daemon *Daemon) getSize(container *container.Container) (int64, int64) { |
101 | 101 |
|
102 | 102 |
// ConnectToNetwork connects a container to a network |
103 | 103 |
func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error { |
104 |
+ if endpointConfig == nil { |
|
105 |
+ endpointConfig = &networktypes.EndpointSettings{} |
|
106 |
+ } |
|
104 | 107 |
if !container.Running { |
105 | 108 |
if container.RemovalInProgress || container.Dead { |
106 | 109 |
return errRemovalContainer(container.ID) |
... | ... |
@@ -108,9 +111,7 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName |
108 | 108 |
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil { |
109 | 109 |
return err |
110 | 110 |
} |
111 |
- if endpointConfig != nil { |
|
112 |
- container.NetworkSettings.Networks[idOrName] = endpointConfig |
|
113 |
- } |
|
111 |
+ container.NetworkSettings.Networks[idOrName] = endpointConfig |
|
114 | 112 |
} else { |
115 | 113 |
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil { |
116 | 114 |
return err |
... | ... |
@@ -14,6 +14,7 @@ import ( |
14 | 14 |
"time" |
15 | 15 |
|
16 | 16 |
"github.com/docker/docker/pkg/integration/checker" |
17 |
+ "github.com/docker/docker/pkg/stringid" |
|
17 | 18 |
"github.com/docker/docker/runconfig" |
18 | 19 |
"github.com/docker/engine-api/types" |
19 | 20 |
"github.com/docker/engine-api/types/versions/v1p20" |
... | ... |
@@ -1348,7 +1349,7 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) { |
1348 | 1348 |
dockerCmd(c, "network", "create", "-d", "bridge", "net1") |
1349 | 1349 |
dockerCmd(c, "network", "create", "-d", "bridge", "net2") |
1350 | 1350 |
|
1351 |
- dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top") |
|
1351 |
+ cid, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top") |
|
1352 | 1352 |
c.Assert(waitRun("first"), check.IsNil) |
1353 | 1353 |
|
1354 | 1354 |
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top") |
... | ... |
@@ -1360,6 +1361,10 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) { |
1360 | 1360 |
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") |
1361 | 1361 |
c.Assert(err, check.IsNil) |
1362 | 1362 |
|
1363 |
+ // ping first container's short-id alias |
|
1364 |
+ _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid)) |
|
1365 |
+ c.Assert(err, check.IsNil) |
|
1366 |
+ |
|
1363 | 1367 |
// connect first container to net2 network |
1364 | 1368 |
dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first") |
1365 | 1369 |
// connect second container to foo2 network with a different alias for first container |
... | ... |
@@ -1379,6 +1384,9 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) { |
1379 | 1379 |
// ping to net2 scoped alias "bar" must still succeed |
1380 | 1380 |
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar") |
1381 | 1381 |
c.Assert(err, check.IsNil) |
1382 |
+ // ping to net2 scoped alias short-id must still succeed |
|
1383 |
+ _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid)) |
|
1384 |
+ c.Assert(err, check.IsNil) |
|
1382 | 1385 |
|
1383 | 1386 |
// verify the alias option is rejected when running on predefined network |
1384 | 1387 |
out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top") |
... | ... |
@@ -20,6 +20,7 @@ import ( |
20 | 20 |
|
21 | 21 |
"github.com/docker/docker/pkg/integration/checker" |
22 | 22 |
"github.com/docker/docker/pkg/mount" |
23 |
+ "github.com/docker/docker/pkg/stringid" |
|
23 | 24 |
"github.com/docker/docker/pkg/stringutils" |
24 | 25 |
"github.com/docker/docker/runconfig" |
25 | 26 |
"github.com/docker/go-connections/nat" |
... | ... |
@@ -285,12 +286,22 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) { |
285 | 285 |
testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm) |
286 | 286 |
dockerCmd(c, "network", "create", "-d", "bridge", "net1") |
287 | 287 |
|
288 |
- dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top") |
|
288 |
+ cid1, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top") |
|
289 | 289 |
c.Assert(waitRun("first"), check.IsNil) |
290 | 290 |
|
291 |
- dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top") |
|
291 |
+ // Check if default short-id alias is added automatically |
|
292 |
+ id := strings.TrimSpace(cid1) |
|
293 |
+ aliases := inspectField(c, id, "NetworkSettings.Networks.net1.Aliases") |
|
294 |
+ c.Assert(aliases, checker.Contains, stringid.TruncateID(id)) |
|
295 |
+ |
|
296 |
+ cid2, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top") |
|
292 | 297 |
c.Assert(waitRun("second"), check.IsNil) |
293 | 298 |
|
299 |
+ // Check if default short-id alias is added automatically |
|
300 |
+ id = strings.TrimSpace(cid2) |
|
301 |
+ aliases = inspectField(c, id, "NetworkSettings.Networks.net1.Aliases") |
|
302 |
+ c.Assert(aliases, checker.Contains, stringid.TruncateID(id)) |
|
303 |
+ |
|
294 | 304 |
// ping to first and its network-scoped aliases |
295 | 305 |
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") |
296 | 306 |
c.Assert(err, check.IsNil) |
... | ... |
@@ -298,6 +309,9 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) { |
298 | 298 |
c.Assert(err, check.IsNil) |
299 | 299 |
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2") |
300 | 300 |
c.Assert(err, check.IsNil) |
301 |
+ // ping first container's short-id alias |
|
302 |
+ _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid1)) |
|
303 |
+ c.Assert(err, check.IsNil) |
|
301 | 304 |
|
302 | 305 |
// Restart first container |
303 | 306 |
dockerCmd(c, "restart", "first") |
... | ... |
@@ -310,6 +324,9 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) { |
310 | 310 |
c.Assert(err, check.IsNil) |
311 | 311 |
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2") |
312 | 312 |
c.Assert(err, check.IsNil) |
313 |
+ // ping first container's short-id alias |
|
314 |
+ _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid1)) |
|
315 |
+ c.Assert(err, check.IsNil) |
|
313 | 316 |
} |
314 | 317 |
|
315 | 318 |
// Issue 9677. |