Browse Source

Merge pull request #31996 from aboch/drst

Fix stop/start/restart of detached container
Brian Goff 8 years ago
parent
commit
d8406fd7a0
2 changed files with 40 additions and 1 deletions
  1. 23 1
      daemon/container_operations.go
  2. 17 0
      integration-cli/docker_cli_restart_test.go

+ 23 - 1
daemon/container_operations.go

@@ -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
 	}
 

+ 17 - 0
integration-cli/docker_cli_restart_test.go

@@ -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")