diff --git a/daemon/container_operations.go b/daemon/container_operations.go index b1dd4c3bb6..db3c7e8518 100644 --- a/daemon/container_operations.go +++ b/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 } diff --git a/integration-cli/docker_cli_restart_test.go b/integration-cli/docker_cli_restart_test.go index 17a296d6b1..57c1323a90 100644 --- a/integration-cli/docker_cli_restart_test.go +++ b/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")