Fix start/restart of detached container

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2017-03-22 02:38:26 -07:00
parent 786f30107b
commit 4ca7d4f0c1
2 changed files with 39 additions and 0 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
}

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