2016-03-18 14:50:19 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-10-05 16:29:56 -04:00
|
|
|
"time"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-02-09 21:57:35 -05:00
|
|
|
"github.com/docker/docker/container"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-10-05 16:29:56 -04:00
|
|
|
"github.com/docker/docker/restartmanager"
|
2016-03-18 14:50:19 -04:00
|
|
|
)
|
|
|
|
|
2017-02-09 21:57:35 -05:00
|
|
|
func (daemon *Daemon) setStateCounter(c *container.Container) {
|
|
|
|
switch c.StateString() {
|
|
|
|
case "paused":
|
|
|
|
stateCtr.set(c.ID, "paused")
|
|
|
|
case "running":
|
|
|
|
stateCtr.set(c.ID, "running")
|
|
|
|
default:
|
|
|
|
stateCtr.set(c.ID, "stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
// StateChanged updates daemon state changes from containerd
|
|
|
|
func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
|
|
|
|
c := daemon.containers.Get(id)
|
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("no such container: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.State {
|
|
|
|
case libcontainerd.StateOOM:
|
|
|
|
// StateOOM is Linux specific and should never be hit on Windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return errors.New("Received StateOOM from libcontainerd on Windows. This should never happen.")
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
daemon.updateHealthMonitor(c)
|
2017-04-06 17:42:10 -04:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "oom")
|
|
|
|
case libcontainerd.StateExit:
|
2016-10-05 16:29:56 -04:00
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Lock()
|
2016-11-14 15:15:09 -05:00
|
|
|
c.StreamConfig.Wait()
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Reset(false)
|
2016-10-05 16:29:56 -04:00
|
|
|
|
Don't create source directory while the daemon is being shutdown, fix #30348
If a container mount the socket the daemon is listening on into
container while the daemon is being shutdown, the socket will
not exist on the host, then daemon will assume it's a directory
and create it on the host, this will cause the daemon can't start
next time.
fix issue https://github.com/moby/moby/issues/30348
To reproduce this issue, you can add following code
```
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
+ "time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/container"
@@ -666,7 +667,8 @@ func (daemon *Daemon) createSpec(c *container.Container) (*libcontainerd.Spec, e
if err := daemon.setupIpcDirs(c); err != nil {
return nil, err
}
-
+ fmt.Printf("===please stop the daemon===\n")
+ time.Sleep(time.Second * 2)
ms, err := daemon.setupMounts(c)
if err != nil {
return nil, err
```
step1 run a container which has `--restart always` and `-v /var/run/docker.sock:/sock`
```
$ docker run -ti --restart always -v /var/run/docker.sock:/sock busybox
/ #
```
step2 exit the the container
```
/ # exit
```
and kill the daemon when you see
```
===please stop the daemon===
```
in the daemon log
The daemon can't restart again and fail with `can't create unix socket /var/run/docker.sock: is a directory`.
Signed-off-by: Lei Jitang <leijitang@huawei.com>
2017-05-22 03:44:01 -04:00
|
|
|
// If daemon is being shutdown, don't let the container restart
|
|
|
|
restart, wait, err := c.RestartManager().ShouldRestart(e.ExitCode, daemon.IsShuttingDown() || c.HasBeenManuallyStopped, time.Since(c.StartedAt))
|
2016-10-05 16:29:56 -04:00
|
|
|
if err == nil && restart {
|
|
|
|
c.RestartCount++
|
|
|
|
c.SetRestarting(platformConstructExitStatus(e))
|
|
|
|
} else {
|
|
|
|
c.SetStopped(platformConstructExitStatus(e))
|
2017-02-14 13:35:20 -05:00
|
|
|
defer daemon.autoRemove(c)
|
2016-10-05 16:29:56 -04:00
|
|
|
}
|
|
|
|
|
2017-03-31 12:33:36 -04:00
|
|
|
// cancel healthcheck here, they will be automatically
|
|
|
|
// restarted if/when the container is started again
|
|
|
|
daemon.stopHealthchecks(c)
|
2016-03-18 14:50:19 -04:00
|
|
|
attributes := map[string]string{
|
|
|
|
"exitCode": strconv.Itoa(int(e.ExitCode)),
|
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(c, "die", attributes)
|
|
|
|
daemon.Cleanup(c)
|
2016-10-05 16:29:56 -04:00
|
|
|
|
|
|
|
if err == nil && restart {
|
|
|
|
go func() {
|
|
|
|
err := <-wait
|
|
|
|
if err == nil {
|
2017-06-08 06:55:20 -04:00
|
|
|
// daemon.netController is initialized when daemon is restoring containers.
|
|
|
|
// But containerStart will use daemon.netController segment.
|
|
|
|
// So to avoid panic at startup process, here must wait util daemon restore done.
|
|
|
|
daemon.waitForStartupDone()
|
2016-09-19 12:01:16 -04:00
|
|
|
if err = daemon.containerStart(c, "", "", false); err != nil {
|
2016-10-29 03:03:26 -04:00
|
|
|
logrus.Debugf("failed to restart container: %+v", err)
|
2016-10-05 16:29:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.SetStopped(platformConstructExitStatus(e))
|
2017-02-14 13:35:20 -05:00
|
|
|
defer daemon.autoRemove(c)
|
2016-10-05 16:29:56 -04:00
|
|
|
if err != restartmanager.ErrRestartCanceled {
|
|
|
|
logrus.Errorf("restartmanger wait error: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
2016-10-05 16:29:56 -04:00
|
|
|
defer c.Unlock()
|
2017-03-27 13:18:53 -04:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-04-01 20:02:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return daemon.postRunProcessing(c, e)
|
2016-03-18 14:50:19 -04:00
|
|
|
case libcontainerd.StateExitProcess:
|
|
|
|
if execConfig := c.ExecCommands.Get(e.ProcessID); execConfig != nil {
|
|
|
|
ec := int(e.ExitCode)
|
2016-10-12 19:56:52 -04:00
|
|
|
execConfig.Lock()
|
|
|
|
defer execConfig.Unlock()
|
2016-03-18 14:50:19 -04:00
|
|
|
execConfig.ExitCode = &ec
|
|
|
|
execConfig.Running = false
|
2016-11-14 15:15:09 -05:00
|
|
|
execConfig.StreamConfig.Wait()
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := execConfig.CloseStreams(); err != nil {
|
2017-01-21 06:35:54 -05:00
|
|
|
logrus.Errorf("failed to cleanup exec %s streams: %s", c.ID, err)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the exec command from the container's store only and not the
|
|
|
|
// daemon's store so that the exec command can be inspected.
|
|
|
|
c.ExecCommands.Delete(execConfig.ID)
|
|
|
|
} else {
|
|
|
|
logrus.Warnf("Ignoring StateExitProcess for %v but no exec command found", e)
|
|
|
|
}
|
|
|
|
case libcontainerd.StateStart, libcontainerd.StateRestore:
|
2016-04-18 05:48:13 -04:00
|
|
|
// Container is already locked in this case
|
2016-03-18 14:50:19 -04:00
|
|
|
c.SetRunning(int(e.Pid), e.State == libcontainerd.StateStart)
|
|
|
|
c.HasBeenManuallyStopped = false
|
2016-09-16 13:41:47 -04:00
|
|
|
c.HasBeenStartedBefore = true
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
2017-04-06 17:42:10 -04:00
|
|
|
daemon.initHealthMonitor(c)
|
2017-03-27 13:18:53 -04:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Reset(false)
|
|
|
|
return err
|
|
|
|
}
|
2017-02-09 21:57:35 -05:00
|
|
|
|
2016-04-06 23:38:12 -04:00
|
|
|
daemon.LogContainerEvent(c, "start")
|
2016-03-18 14:50:19 -04:00
|
|
|
case libcontainerd.StatePause:
|
2016-04-18 05:48:13 -04:00
|
|
|
// Container is already locked in this case
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Paused = true
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
2017-04-06 17:42:10 -04:00
|
|
|
daemon.updateHealthMonitor(c)
|
2017-03-27 13:18:53 -04:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-08-19 05:12:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "pause")
|
|
|
|
case libcontainerd.StateResume:
|
2016-04-18 05:48:13 -04:00
|
|
|
// Container is already locked in this case
|
2016-03-18 14:50:19 -04:00
|
|
|
c.Paused = false
|
2017-02-09 21:57:35 -05:00
|
|
|
daemon.setStateCounter(c)
|
2017-04-06 17:42:10 -04:00
|
|
|
daemon.updateHealthMonitor(c)
|
2017-03-27 13:18:53 -04:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-08-19 05:12:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "unpause")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
|
|
|
|
func (daemon *Daemon) autoRemove(c *container.Container) {
|
|
|
|
c.Lock()
|
|
|
|
ar := c.HostConfig.AutoRemove
|
|
|
|
c.Unlock()
|
|
|
|
if !ar {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if err = daemon.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c := daemon.containers.Get(c.ID); c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("container", c.ID).Error("error removing container")
|
|
|
|
}
|
|
|
|
}
|