2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
import (
|
2017-09-22 09:52:41 -04:00
|
|
|
"context"
|
2016-03-18 14:50:19 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-10-05 16:29:56 -04:00
|
|
|
"time"
|
2016-03-18 14:50:19 -04:00
|
|
|
|
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"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// ProcessEvent is called by libcontainerd whenever an event occurs
|
|
|
|
func (daemon *Daemon) ProcessEvent(id string, e libcontainerd.EventType, ei libcontainerd.EventInfo) error {
|
|
|
|
c, err := daemon.GetContainer(id)
|
|
|
|
if c == nil || err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
return fmt.Errorf("no such container: %s", id)
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
switch e {
|
|
|
|
case libcontainerd.EventOOM:
|
2016-03-18 14:50:19 -04:00
|
|
|
// StateOOM is Linux specific and should never be hit on Windows
|
|
|
|
if runtime.GOOS == "windows" {
|
2017-08-17 15:16:30 -04:00
|
|
|
return errors.New("received StateOOM from libcontainerd on Windows. This should never happen")
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2017-11-14 20:59:40 -05:00
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
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
|
|
|
|
}
|
2017-11-14 20:59:40 -05:00
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
daemon.LogContainerEvent(c, "oom")
|
2017-09-22 09:52:41 -04:00
|
|
|
case libcontainerd.EventExit:
|
|
|
|
if int(ei.Pid) == c.Pid {
|
2017-11-15 22:19:26 -05:00
|
|
|
c.Lock()
|
2017-09-22 09:52:41 -04:00
|
|
|
_, _, err := daemon.containerd.DeleteTask(context.Background(), c.ID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Warnf("failed to delete container %s from containerd", c.ID)
|
|
|
|
}
|
2016-10-05 16:29:56 -04:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
c.StreamConfig.Wait()
|
|
|
|
c.Reset(false)
|
2016-10-05 16:29:56 -04:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
exitStatus := container.ExitStatus{
|
|
|
|
ExitCode: int(ei.ExitCode),
|
|
|
|
ExitedAt: ei.ExitedAt,
|
|
|
|
OOMKilled: ei.OOMKilled,
|
|
|
|
}
|
|
|
|
restart, wait, err := c.RestartManager().ShouldRestart(ei.ExitCode, daemon.IsShuttingDown() || c.HasBeenManuallyStopped, time.Since(c.StartedAt))
|
|
|
|
if err == nil && restart {
|
|
|
|
c.RestartCount++
|
|
|
|
c.SetRestarting(&exitStatus)
|
|
|
|
} else {
|
|
|
|
c.SetStopped(&exitStatus)
|
|
|
|
defer daemon.autoRemove(c)
|
|
|
|
}
|
2017-11-15 22:19:26 -05:00
|
|
|
defer c.Unlock() // needs to be called before autoRemove
|
2017-09-22 09:52:41 -04:00
|
|
|
|
|
|
|
// cancel healthcheck here, they will be automatically
|
|
|
|
// restarted if/when the container is started again
|
|
|
|
daemon.stopHealthchecks(c)
|
|
|
|
attributes := map[string]string{
|
|
|
|
"exitCode": strconv.Itoa(int(ei.ExitCode)),
|
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(c, "die", attributes)
|
|
|
|
daemon.Cleanup(c)
|
|
|
|
|
|
|
|
if err == nil && restart {
|
|
|
|
go func() {
|
|
|
|
err := <-wait
|
|
|
|
if err == nil {
|
|
|
|
// 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()
|
|
|
|
if err = daemon.containerStart(c, "", "", false); err != nil {
|
|
|
|
logrus.Debugf("failed to restart container: %+v", err)
|
|
|
|
}
|
2016-10-05 16:29:56 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
if err != nil {
|
2017-11-15 22:19:26 -05:00
|
|
|
c.Lock()
|
2017-09-22 09:52:41 -04:00
|
|
|
c.SetStopped(&exitStatus)
|
2017-11-15 22:19:26 -05:00
|
|
|
c.Unlock()
|
2017-09-22 09:52:41 -04:00
|
|
|
defer daemon.autoRemove(c)
|
|
|
|
if err != restartmanager.ErrRestartCanceled {
|
|
|
|
logrus.Errorf("restartmanger wait error: %+v", err)
|
|
|
|
}
|
2016-10-05 16:29:56 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
}()
|
|
|
|
}
|
2017-02-09 21:57:35 -05:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
daemon.setStateCounter(c)
|
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return daemon.postRunProcessing(c, ei)
|
2016-04-01 20:02:38 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
|
2017-11-27 10:53:16 -05:00
|
|
|
if execConfig := c.ExecCommands.Get(ei.ProcessID); execConfig != nil {
|
2017-09-22 09:52:41 -04:00
|
|
|
ec := int(ei.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.
|
2017-09-22 09:52:41 -04:00
|
|
|
c.ExecCommands.Delete(execConfig.ID, execConfig.Pid)
|
2017-12-08 03:01:34 -05:00
|
|
|
attributes := map[string]string{
|
|
|
|
"execID": execConfig.ID,
|
|
|
|
"exitCode": strconv.Itoa(ec),
|
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(c, "exec_die", attributes)
|
2016-03-18 14:50:19 -04:00
|
|
|
} else {
|
2017-09-22 09:52:41 -04:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"container": c.ID,
|
2017-11-27 10:53:16 -05:00
|
|
|
"exec-id": ei.ProcessID,
|
2017-09-22 09:52:41 -04:00
|
|
|
"exec-pid": ei.Pid,
|
|
|
|
}).Warnf("Ignoring Exit Event, no such exec command found")
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
case libcontainerd.EventStart:
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
// This is here to handle start not generated by docker
|
|
|
|
if !c.Running {
|
|
|
|
c.SetRunning(int(ei.Pid), false)
|
|
|
|
c.HasBeenManuallyStopped = false
|
|
|
|
c.HasBeenStartedBefore = true
|
|
|
|
daemon.setStateCounter(c)
|
|
|
|
|
|
|
|
daemon.initHealthMonitor(c)
|
|
|
|
|
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
daemon.LogContainerEvent(c, "start")
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2017-02-09 21:57:35 -05:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
case libcontainerd.EventPaused:
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if !c.Paused {
|
|
|
|
c.Paused = true
|
|
|
|
daemon.setStateCounter(c)
|
|
|
|
daemon.updateHealthMonitor(c)
|
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
daemon.LogContainerEvent(c, "pause")
|
2016-08-19 05:12:01 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
case libcontainerd.EventResumed:
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.Paused {
|
|
|
|
c.Paused = false
|
|
|
|
daemon.setStateCounter(c)
|
|
|
|
daemon.updateHealthMonitor(c)
|
|
|
|
|
|
|
|
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
daemon.LogContainerEvent(c, "unpause")
|
2016-08-19 05:12:01 -04:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|