2016-03-18 18:50:19 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-10-05 20:29:56 +00:00
|
|
|
"time"
|
2016-03-18 18:50:19 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-02-10 02:57:35 +00:00
|
|
|
"github.com/docker/docker/container"
|
2016-03-18 18:50:19 +00:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-10-05 20:29:56 +00:00
|
|
|
"github.com/docker/docker/restartmanager"
|
2016-03-18 18:50:19 +00:00
|
|
|
)
|
|
|
|
|
2017-02-10 02:57:35 +00: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 18:50:19 +00: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 09:48:13 +00:00
|
|
|
daemon.updateHealthMonitor(c)
|
2017-04-06 21:42:10 +00:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
daemon.LogContainerEvent(c, "oom")
|
|
|
|
case libcontainerd.StateExit:
|
2016-10-05 20:29:56 +00:00
|
|
|
|
2016-03-18 18:50:19 +00:00
|
|
|
c.Lock()
|
2016-11-14 20:15:09 +00:00
|
|
|
c.StreamConfig.Wait()
|
2016-03-18 18:50:19 +00:00
|
|
|
c.Reset(false)
|
2016-10-05 20:29:56 +00: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 07:44:01 +00: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 20:29:56 +00:00
|
|
|
if err == nil && restart {
|
|
|
|
c.RestartCount++
|
|
|
|
c.SetRestarting(platformConstructExitStatus(e))
|
|
|
|
} else {
|
|
|
|
c.SetStopped(platformConstructExitStatus(e))
|
2017-02-14 18:35:20 +00:00
|
|
|
defer daemon.autoRemove(c)
|
2016-10-05 20:29:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 16:33:36 +00:00
|
|
|
// cancel healthcheck here, they will be automatically
|
|
|
|
// restarted if/when the container is started again
|
|
|
|
daemon.stopHealthchecks(c)
|
2016-03-18 18:50:19 +00:00
|
|
|
attributes := map[string]string{
|
|
|
|
"exitCode": strconv.Itoa(int(e.ExitCode)),
|
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(c, "die", attributes)
|
|
|
|
daemon.Cleanup(c)
|
2016-10-05 20:29:56 +00:00
|
|
|
|
|
|
|
if err == nil && restart {
|
|
|
|
go func() {
|
|
|
|
err := <-wait
|
|
|
|
if err == nil {
|
2017-06-08 10:55:20 +00: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 16:01:16 +00:00
|
|
|
if err = daemon.containerStart(c, "", "", false); err != nil {
|
2016-10-29 07:03:26 +00:00
|
|
|
logrus.Debugf("failed to restart container: %+v", err)
|
2016-10-05 20:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.SetStopped(platformConstructExitStatus(e))
|
2017-02-14 18:35:20 +00:00
|
|
|
defer daemon.autoRemove(c)
|
2016-10-05 20:29:56 +00:00
|
|
|
if err != restartmanager.ErrRestartCanceled {
|
|
|
|
logrus.Errorf("restartmanger wait error: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:57:35 +00:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
2016-10-05 20:29:56 +00:00
|
|
|
defer c.Unlock()
|
2017-03-27 17:18:53 +00:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-04-02 00:02:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return daemon.postRunProcessing(c, e)
|
2016-03-18 18:50:19 +00:00
|
|
|
case libcontainerd.StateExitProcess:
|
|
|
|
if execConfig := c.ExecCommands.Get(e.ProcessID); execConfig != nil {
|
|
|
|
ec := int(e.ExitCode)
|
2016-10-12 23:56:52 +00:00
|
|
|
execConfig.Lock()
|
|
|
|
defer execConfig.Unlock()
|
2016-03-18 18:50:19 +00:00
|
|
|
execConfig.ExitCode = &ec
|
|
|
|
execConfig.Running = false
|
2016-11-14 20:15:09 +00:00
|
|
|
execConfig.StreamConfig.Wait()
|
2016-03-18 18:50:19 +00:00
|
|
|
if err := execConfig.CloseStreams(); err != nil {
|
2017-01-21 11:35:54 +00:00
|
|
|
logrus.Errorf("failed to cleanup exec %s streams: %s", c.ID, err)
|
2016-03-18 18:50:19 +00: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 09:48:13 +00:00
|
|
|
// Container is already locked in this case
|
2016-03-18 18:50:19 +00:00
|
|
|
c.SetRunning(int(e.Pid), e.State == libcontainerd.StateStart)
|
|
|
|
c.HasBeenManuallyStopped = false
|
2016-09-16 17:41:47 +00:00
|
|
|
c.HasBeenStartedBefore = true
|
2017-02-10 02:57:35 +00:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
2017-04-06 21:42:10 +00:00
|
|
|
daemon.initHealthMonitor(c)
|
2017-03-27 17:18:53 +00:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-03-18 18:50:19 +00:00
|
|
|
c.Reset(false)
|
|
|
|
return err
|
|
|
|
}
|
2017-02-10 02:57:35 +00:00
|
|
|
|
2016-04-07 03:38:12 +00:00
|
|
|
daemon.LogContainerEvent(c, "start")
|
2016-03-18 18:50:19 +00:00
|
|
|
case libcontainerd.StatePause:
|
2016-04-18 09:48:13 +00:00
|
|
|
// Container is already locked in this case
|
2016-03-18 18:50:19 +00:00
|
|
|
c.Paused = true
|
2017-02-10 02:57:35 +00:00
|
|
|
daemon.setStateCounter(c)
|
2017-04-06 21:42:10 +00:00
|
|
|
daemon.updateHealthMonitor(c)
|
2017-03-27 17:18:53 +00:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-08-19 09:12:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
daemon.LogContainerEvent(c, "pause")
|
|
|
|
case libcontainerd.StateResume:
|
2016-04-18 09:48:13 +00:00
|
|
|
// Container is already locked in this case
|
2016-03-18 18:50:19 +00:00
|
|
|
c.Paused = false
|
2017-02-10 02:57:35 +00:00
|
|
|
daemon.setStateCounter(c)
|
2017-04-06 21:42:10 +00:00
|
|
|
daemon.updateHealthMonitor(c)
|
2017-03-27 17:18:53 +00:00
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
2016-08-19 09:12:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
daemon.LogContainerEvent(c, "unpause")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-14 18:35:20 +00: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")
|
|
|
|
}
|
|
|
|
}
|