Store endpoint config on network connect to a stopped container
Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
parent
a315a5acec
commit
fa0856e0e4
2 changed files with 44 additions and 0 deletions
|
@ -744,6 +744,9 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName
|
|||
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if endpointConfig != nil {
|
||||
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
||||
}
|
||||
} else {
|
||||
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
|
||||
return err
|
||||
|
|
|
@ -1123,10 +1123,12 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
|
|||
// run a container on first network specifying the ip addresses
|
||||
dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
|
||||
c.Assert(waitRun("c0"), check.IsNil)
|
||||
verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
|
||||
verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
|
||||
|
||||
// connect the container to the second network specifying the preferred ip addresses
|
||||
dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n1", "c0")
|
||||
verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
|
||||
// Stop and restart the container
|
||||
|
@ -1134,7 +1136,9 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
|
|||
dockerCmd(c, "start", "c0")
|
||||
|
||||
// verify preferred addresses are applied
|
||||
verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
|
||||
verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
|
||||
verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
|
||||
// Still it should fail to connect to the default network with a specified IP (whatever ip)
|
||||
|
@ -1144,6 +1148,29 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
|
|||
|
||||
}
|
||||
|
||||
func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIPStoppedContainer(c *check.C) {
|
||||
// create a container
|
||||
dockerCmd(c, "create", "--name", "c0", "busybox", "top")
|
||||
|
||||
// create a network
|
||||
dockerCmd(c, "network", "create", "--subnet=172.30.0.0/16", "--subnet=2001:db8:abcd::/64", "n0")
|
||||
assertNwIsAvailable(c, "n0")
|
||||
|
||||
// connect the container to the network specifying an ip addresses
|
||||
dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n0", "c0")
|
||||
verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
|
||||
// start the container, verify config has not changed and ip addresses are assigned
|
||||
dockerCmd(c, "start", "c0")
|
||||
c.Assert(waitRun("c0"), check.IsNil)
|
||||
verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
verifyIPAddresses(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
|
||||
// stop the container and check ip config has not changed
|
||||
dockerCmd(c, "stop", "c0")
|
||||
verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
|
||||
}
|
||||
|
||||
func (s *DockerNetworkSuite) TestDockerNetworkUnsupportedPreferredIP(c *check.C) {
|
||||
// preferred IP is not supported on predefined networks
|
||||
for _, mode := range []string{"none", "host", "bridge", "default"} {
|
||||
|
@ -1172,6 +1199,20 @@ func checkUnsupportedNetworkAndIP(c *check.C, nwMode string) {
|
|||
c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
|
||||
}
|
||||
|
||||
func verifyIPAddressConfig(c *check.C, cName, nwname, ipv4, ipv6 string) {
|
||||
if ipv4 != "" {
|
||||
out, err := inspectField(cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv4Address", nwname))
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
|
||||
}
|
||||
|
||||
if ipv6 != "" {
|
||||
out, err := inspectField(cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv6Address", nwname))
|
||||
c.Assert(err, checker.IsNil)
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
|
||||
}
|
||||
}
|
||||
|
||||
func verifyIPAddresses(c *check.C, cName, nwname, ipv4, ipv6 string) {
|
||||
out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", nwname), cName)
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
|
||||
|
|
Loading…
Add table
Reference in a new issue