1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #18531 from coolljt0725/update_container_networking_on_create

fix docker inspect return unconsistent network settings  of created container and stopped container
This commit is contained in:
Alexander Morozov 2015-12-15 08:47:54 -08:00
commit 812a7c204a
4 changed files with 55 additions and 14 deletions

View file

@ -529,6 +529,29 @@ func (daemon *Daemon) updateNetwork(container *container.Container) error {
return nil
}
// updateContainerNetworkSettings update the network settings
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container) error {
mode := container.HostConfig.NetworkMode
if container.Config.NetworkDisabled || mode.IsContainer() {
return nil
}
networkName := mode.NetworkName()
if mode.IsDefault() {
networkName = daemon.netController.Config().Daemon.DefaultNetwork
}
if mode.IsUserDefined() {
n, err := daemon.FindNetwork(networkName)
if err != nil {
return err
}
networkName = n.Name()
}
container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
return nil
}
func (daemon *Daemon) allocateNetwork(container *container.Container) error {
controller := daemon.netController
@ -539,24 +562,14 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {
updateSettings := false
if len(container.NetworkSettings.Networks) == 0 {
mode := container.HostConfig.NetworkMode
if container.Config.NetworkDisabled || mode.IsContainer() {
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
return nil
}
networkName := mode.NetworkName()
if mode.IsDefault() {
networkName = controller.Config().Daemon.DefaultNetwork
err := daemon.updateContainerNetworkSettings(container)
if err != nil {
return err
}
if mode.IsUserDefined() {
n, err := daemon.FindNetwork(networkName)
if err != nil {
return err
}
networkName = n.Name()
}
container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
updateSettings = true
}

View file

@ -16,6 +16,11 @@ func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]s
return nil, nil
}
// updateContainerNetworkSettings update the network settings
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container) error {
return nil
}
func (daemon *Daemon) initializeNetworking(container *container.Container) error {
return nil
}

View file

@ -106,6 +106,10 @@ func (daemon *Daemon) create(params *ContainerCreateConfig) (retC *container.Con
return nil, err
}
if err := daemon.updateContainerNetworkSettings(container); err != nil {
return nil, err
}
if err := container.ToDiskLocking(); err != nil {
logrus.Errorf("Error saving new container to disk: %v", err)
return nil, err

View file

@ -897,3 +897,22 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectWithMac(c *check.C) {
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(mac2), checker.Not(checker.Equals), strings.TrimSpace(mac1))
}
func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C) {
dockerCmd(c, "create", "--name", "test", "busybox")
networks, err := inspectField("test", "NetworkSettings.Networks")
c.Assert(err, checker.IsNil)
c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
}
func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMulipleNetworks(c *check.C) {
dockerCmd(c, "network", "create", "test")
dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
c.Assert(waitRun("foo"), checker.IsNil)
dockerCmd(c, "network", "connect", "test", "foo")
dockerCmd(c, "restart", "foo")
networks, err := inspectField("foo", "NetworkSettings.Networks")
c.Assert(err, checker.IsNil)
c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should contain 'bridge' network"))
c.Assert(networks, checker.Contains, "test", check.Commentf("Should contain 'test' netwokr"))
}