Merge pull request #31996 from aboch/drst

Fix stop/start/restart of detached container
This commit is contained in:
Brian Goff 2017-03-22 10:24:45 -04:00 committed by GitHub
commit d8406fd7a0
2 changed files with 40 additions and 1 deletions

View file

@ -546,6 +546,28 @@ func (daemon *Daemon) allocateNetwork(container *container.Container) error {
}
}
// If the container is not to be connected to any network,
// create its network sandbox now if not present
if len(networks) == 0 {
if nil == daemon.getNetworkSandbox(container) {
options, err := daemon.buildSandboxOptions(container)
if err != nil {
return err
}
sb, err := daemon.netController.NewSandbox(container.ID, options...)
if err != nil {
return err
}
container.UpdateSandboxNetworkSettings(sb)
defer func() {
if err != nil {
sb.Delete()
}
}()
}
}
if err := container.WriteHostConfig(); err != nil {
return err
}
@ -916,7 +938,7 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
settings := container.NetworkSettings.Networks
container.NetworkSettings.Ports = nil
if sid == "" || len(settings) == 0 {
if sid == "" {
return
}

View file

@ -75,6 +75,23 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
c.Assert(source, checker.Equals, sourceAfterRestart)
}
func (s *DockerSuite) TestRestartDisconnectedContainer(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
// Run a container on the default bridge network
out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
cleanedContainerID := strings.TrimSpace(out)
c.Assert(waitRun(cleanedContainerID), checker.IsNil)
// Disconnect the container from the network
out, err := dockerCmd(c, "network", "disconnect", "bridge", "c0")
c.Assert(err, check.NotNil, check.Commentf(out))
// Restart the container
dockerCmd(c, "restart", "c0")
c.Assert(err, check.NotNil, check.Commentf(out))
}
func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
out, _ := dockerCmd(c, "create", "--restart=no", "busybox")