2014-08-04 20:05:56 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-24 18:11:48 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-08-04 20:05:56 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2014-08-04 21:20:53 -04:00
|
|
|
const defaultTimeIncrement = 100
|
|
|
|
|
2014-08-04 20:05:56 -04:00
|
|
|
// containerMonitor monitors the execution of a container's main process.
|
2015-01-05 22:33:53 -05:00
|
|
|
// If a restart policy is specified for the container the monitor will ensure that the
|
2014-08-04 20:05:56 -04:00
|
|
|
// process is restarted based on the rules of the policy. When the container is finally stopped
|
|
|
|
// the monitor will reset and cleanup any of the container resources such as networking allocations
|
|
|
|
// and the rootfs
|
|
|
|
type containerMonitor struct {
|
|
|
|
mux sync.Mutex
|
|
|
|
|
2014-08-04 21:20:53 -04:00
|
|
|
// container is the container being monitored
|
|
|
|
container *Container
|
|
|
|
|
2014-08-13 17:56:35 -04:00
|
|
|
// restartPolicy is the current policy being applied to the container monitor
|
2014-08-04 20:05:56 -04:00
|
|
|
restartPolicy runconfig.RestartPolicy
|
2014-08-04 21:20:53 -04:00
|
|
|
|
|
|
|
// failureCount is the number of times the container has failed to
|
|
|
|
// start in a row
|
|
|
|
failureCount int
|
|
|
|
|
|
|
|
// shouldStop signals the monitor that the next time the container exits it is
|
|
|
|
// either because docker or the user asked for the container to be stopped
|
|
|
|
shouldStop bool
|
|
|
|
|
2014-08-13 17:56:35 -04:00
|
|
|
// startSignal is a channel that is closes after the container initially starts
|
2014-08-12 21:03:11 -04:00
|
|
|
startSignal chan struct{}
|
|
|
|
|
2014-08-11 18:00:55 -04:00
|
|
|
// stopChan is used to signal to the monitor whenever there is a wait for the
|
|
|
|
// next restart so that the timeIncrement is not honored and the user is not
|
|
|
|
// left waiting for nothing to happen during this time
|
|
|
|
stopChan chan struct{}
|
|
|
|
|
2014-08-04 21:20:53 -04:00
|
|
|
// timeIncrement is the amount of time to wait between restarts
|
|
|
|
// this is in milliseconds
|
|
|
|
timeIncrement int
|
2014-08-12 16:08:26 -04:00
|
|
|
|
|
|
|
// lastStartTime is the time which the monitor last exec'd the container's process
|
|
|
|
lastStartTime time.Time
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
|
2014-08-12 21:03:11 -04:00
|
|
|
// newContainerMonitor returns an initialized containerMonitor for the provided container
|
|
|
|
// honoring the provided restart policy
|
2014-08-04 20:05:56 -04:00
|
|
|
func newContainerMonitor(container *Container, policy runconfig.RestartPolicy) *containerMonitor {
|
|
|
|
return &containerMonitor{
|
|
|
|
container: container,
|
|
|
|
restartPolicy: policy,
|
2014-08-04 21:20:53 -04:00
|
|
|
timeIncrement: defaultTimeIncrement,
|
2014-08-14 13:50:22 -04:00
|
|
|
stopChan: make(chan struct{}),
|
|
|
|
startSignal: make(chan struct{}),
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals to the container monitor that it should stop monitoring the container
|
|
|
|
// for exits the next time the process dies
|
|
|
|
func (m *containerMonitor) ExitOnNext() {
|
|
|
|
m.mux.Lock()
|
2014-08-11 18:00:55 -04:00
|
|
|
|
|
|
|
// we need to protect having a double close of the channel when stop is called
|
|
|
|
// twice or else we will get a panic
|
|
|
|
if !m.shouldStop {
|
|
|
|
m.shouldStop = true
|
|
|
|
close(m.stopChan)
|
|
|
|
}
|
|
|
|
|
2014-08-04 20:05:56 -04:00
|
|
|
m.mux.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the container's resources such as networking allocations and
|
|
|
|
// unmounts the contatiner's root filesystem
|
|
|
|
func (m *containerMonitor) Close() error {
|
|
|
|
// Cleanup networking and mounts
|
|
|
|
m.container.cleanup()
|
|
|
|
|
2014-08-06 13:40:43 -04:00
|
|
|
// FIXME: here is race condition between two RUN instructions in Dockerfile
|
|
|
|
// because they share same runconfig and change image. Must be fixed
|
|
|
|
// in builder/builder.go
|
|
|
|
if err := m.container.toDisk(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error dumping container %s state to disk: %s", m.container.ID, err)
|
2014-08-06 13:40:43 -04:00
|
|
|
|
|
|
|
return err
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the containers process and monitors it according to the restart policy
|
|
|
|
func (m *containerMonitor) Start() error {
|
|
|
|
var (
|
2014-08-04 21:20:53 -04:00
|
|
|
err error
|
2014-10-30 19:06:54 -04:00
|
|
|
exitStatus execdriver.ExitStatus
|
2014-08-26 11:53:43 -04:00
|
|
|
// this variable indicates where we in execution flow:
|
|
|
|
// before Run or after
|
|
|
|
afterRun bool
|
2014-08-04 20:05:56 -04:00
|
|
|
)
|
2014-08-04 21:20:53 -04:00
|
|
|
|
|
|
|
// ensure that when the monitor finally exits we release the networking and unmount the rootfs
|
2014-08-14 07:51:31 -04:00
|
|
|
defer func() {
|
2014-08-26 11:53:43 -04:00
|
|
|
if afterRun {
|
2014-08-14 07:51:31 -04:00
|
|
|
m.container.Lock()
|
2014-10-30 19:06:54 -04:00
|
|
|
m.container.setStopped(&exitStatus)
|
2014-08-14 07:51:31 -04:00
|
|
|
defer m.container.Unlock()
|
|
|
|
}
|
|
|
|
m.Close()
|
|
|
|
}()
|
2014-08-04 20:05:56 -04:00
|
|
|
|
|
|
|
// reset the restart count
|
|
|
|
m.container.RestartCount = -1
|
|
|
|
|
2014-08-11 18:00:55 -04:00
|
|
|
for {
|
2014-08-04 20:05:56 -04:00
|
|
|
m.container.RestartCount++
|
2014-08-04 21:20:53 -04:00
|
|
|
|
2015-02-04 14:04:58 -05:00
|
|
|
if err := m.container.startLogging(); err != nil {
|
2014-08-28 07:39:27 -04:00
|
|
|
m.resetContainer(false)
|
2014-08-04 20:05:56 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
pipes := execdriver.NewPipes(m.container.stdin, m.container.stdout, m.container.stderr, m.container.Config.OpenStdin)
|
2014-08-04 20:05:56 -04:00
|
|
|
|
2014-08-07 13:50:25 -04:00
|
|
|
m.container.LogEvent("start")
|
|
|
|
|
2014-08-12 16:08:26 -04:00
|
|
|
m.lastStartTime = time.Now()
|
|
|
|
|
2014-08-04 21:20:53 -04:00
|
|
|
if exitStatus, err = m.container.daemon.Run(m.container, pipes, m.callback); err != nil {
|
2014-08-12 21:03:11 -04:00
|
|
|
// if we receive an internal error from the initial start of a container then lets
|
|
|
|
// return it instead of entering the restart loop
|
2014-08-13 17:58:57 -04:00
|
|
|
if m.container.RestartCount == 0 {
|
2014-10-30 19:06:54 -04:00
|
|
|
m.container.ExitCode = -1
|
2014-08-28 07:39:27 -04:00
|
|
|
m.resetContainer(false)
|
2014-08-12 21:03:11 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error running container: %s", err)
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
|
2014-08-14 07:51:31 -04:00
|
|
|
// here container.Lock is already lost
|
2014-08-26 11:53:43 -04:00
|
|
|
afterRun = true
|
2014-08-14 07:51:31 -04:00
|
|
|
|
2014-10-08 13:03:57 -04:00
|
|
|
m.resetMonitor(err == nil && exitStatus.ExitCode == 0)
|
2014-08-04 20:05:56 -04:00
|
|
|
|
2014-10-08 13:03:57 -04:00
|
|
|
if m.shouldRestart(exitStatus.ExitCode) {
|
2014-10-30 19:06:54 -04:00
|
|
|
m.container.SetRestarting(&exitStatus)
|
2014-11-19 14:50:16 -05:00
|
|
|
if exitStatus.OOMKilled {
|
|
|
|
m.container.LogEvent("oom")
|
|
|
|
}
|
2014-08-11 18:00:55 -04:00
|
|
|
m.container.LogEvent("die")
|
2014-08-28 07:39:27 -04:00
|
|
|
m.resetContainer(true)
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2014-08-11 13:50:02 -04:00
|
|
|
// sleep with a small time increment between each restart to help avoid issues cased by quickly
|
|
|
|
// restarting the container because of some types of errors ( networking cut out, etc... )
|
2014-08-11 18:00:55 -04:00
|
|
|
m.waitForNextRestart()
|
|
|
|
|
|
|
|
// we need to check this before reentering the loop because the waitForNextRestart could have
|
|
|
|
// been terminated by a request from a user
|
|
|
|
if m.shouldStop {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-04 20:05:56 -04:00
|
|
|
continue
|
|
|
|
}
|
2014-11-19 14:50:16 -05:00
|
|
|
if exitStatus.OOMKilled {
|
|
|
|
m.container.LogEvent("oom")
|
|
|
|
}
|
2014-08-11 18:00:55 -04:00
|
|
|
m.container.LogEvent("die")
|
2014-08-28 07:39:27 -04:00
|
|
|
m.resetContainer(true)
|
2014-08-26 11:53:43 -04:00
|
|
|
return err
|
2014-08-11 18:00:55 -04:00
|
|
|
}
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
|
2014-08-11 14:07:37 -04:00
|
|
|
// resetMonitor resets the stateful fields on the containerMonitor based on the
|
2015-03-13 05:12:02 -04:00
|
|
|
// previous runs success or failure. Regardless of success, if the container had
|
2014-08-12 16:08:26 -04:00
|
|
|
// an execution time of more than 10s then reset the timer back to the default
|
2014-08-11 14:07:37 -04:00
|
|
|
func (m *containerMonitor) resetMonitor(successful bool) {
|
2014-08-12 16:08:26 -04:00
|
|
|
executionTime := time.Now().Sub(m.lastStartTime).Seconds()
|
|
|
|
|
|
|
|
if executionTime > 10 {
|
2014-08-11 14:07:37 -04:00
|
|
|
m.timeIncrement = defaultTimeIncrement
|
|
|
|
} else {
|
|
|
|
// otherwise we need to increment the amount of time we wait before restarting
|
|
|
|
// the process. We will build up by multiplying the increment by 2
|
2014-08-12 16:08:26 -04:00
|
|
|
m.timeIncrement *= 2
|
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2014-08-12 16:08:26 -04:00
|
|
|
// the container exited successfully so we need to reset the failure counter
|
|
|
|
if successful {
|
|
|
|
m.failureCount = 0
|
|
|
|
} else {
|
2014-08-11 14:07:37 -04:00
|
|
|
m.failureCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 18:00:55 -04:00
|
|
|
// waitForNextRestart waits with the default time increment to restart the container unless
|
2014-08-13 17:56:35 -04:00
|
|
|
// a user or docker asks for the container to be stopped
|
2014-08-11 18:00:55 -04:00
|
|
|
func (m *containerMonitor) waitForNextRestart() {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Duration(m.timeIncrement) * time.Millisecond):
|
|
|
|
case <-m.stopChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 21:20:53 -04:00
|
|
|
// shouldRestart checks the restart policy and applies the rules to determine if
|
|
|
|
// the container's process should be restarted
|
2014-10-08 13:03:57 -04:00
|
|
|
func (m *containerMonitor) shouldRestart(exitCode int) bool {
|
2014-08-04 20:05:56 -04:00
|
|
|
m.mux.Lock()
|
2014-08-04 21:20:53 -04:00
|
|
|
defer m.mux.Unlock()
|
|
|
|
|
|
|
|
// do not restart if the user or docker has requested that this container be stopped
|
|
|
|
if m.shouldStop {
|
|
|
|
return false
|
|
|
|
}
|
2014-08-04 20:05:56 -04:00
|
|
|
|
2015-05-16 07:34:09 -04:00
|
|
|
switch {
|
|
|
|
case m.restartPolicy.IsAlways():
|
2014-08-04 21:20:53 -04:00
|
|
|
return true
|
2015-05-16 07:34:09 -04:00
|
|
|
case m.restartPolicy.IsOnFailure():
|
2014-08-04 21:20:53 -04:00
|
|
|
// the default value of 0 for MaximumRetryCount means that we will not enforce a maximum count
|
2015-01-07 02:16:04 -05:00
|
|
|
if max := m.restartPolicy.MaximumRetryCount; max != 0 && m.failureCount > max {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("stopping restart of container %s because maximum failure could of %d has been reached",
|
2015-03-24 07:25:26 -04:00
|
|
|
stringid.TruncateID(m.container.ID), max)
|
2014-08-04 21:20:53 -04:00
|
|
|
return false
|
|
|
|
}
|
2014-08-04 20:05:56 -04:00
|
|
|
|
2014-10-08 13:03:57 -04:00
|
|
|
return exitCode != 0
|
2014-08-04 21:20:53 -04:00
|
|
|
}
|
2014-08-04 20:05:56 -04:00
|
|
|
|
2014-08-04 21:20:53 -04:00
|
|
|
return false
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// callback ensures that the container's state is properly updated after we
|
|
|
|
// received ack from the execution drivers
|
2014-08-26 18:44:00 -04:00
|
|
|
func (m *containerMonitor) callback(processConfig *execdriver.ProcessConfig, pid int) {
|
2014-08-26 18:05:37 -04:00
|
|
|
if processConfig.Tty {
|
2014-08-04 20:05:56 -04:00
|
|
|
// The callback is called after the process Start()
|
2014-09-28 11:01:35 -04:00
|
|
|
// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
|
2014-08-04 20:05:56 -04:00
|
|
|
// which we close here.
|
2014-08-26 18:05:37 -04:00
|
|
|
if c, ok := processConfig.Stdout.(io.Closer); ok {
|
2014-08-04 20:05:56 -04:00
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
m.container.setRunning(pid)
|
2014-08-04 20:05:56 -04:00
|
|
|
|
2014-08-14 03:01:39 -04:00
|
|
|
// signal that the process has started
|
|
|
|
// close channel only if not closed
|
|
|
|
select {
|
|
|
|
case <-m.startSignal:
|
|
|
|
default:
|
2014-08-13 17:56:35 -04:00
|
|
|
close(m.startSignal)
|
|
|
|
}
|
2014-08-12 21:03:11 -04:00
|
|
|
|
2014-08-04 20:05:56 -04:00
|
|
|
if err := m.container.ToDisk(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("%s", err)
|
2014-08-04 20:05:56 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
|
|
|
// resetContainer resets the container's IO and ensures that the command is able to be executed again
|
|
|
|
// by copying the data into a new struct
|
2014-08-28 07:39:27 -04:00
|
|
|
// if lock is true, then container locked during reset
|
|
|
|
func (m *containerMonitor) resetContainer(lock bool) {
|
2014-08-11 14:07:37 -04:00
|
|
|
container := m.container
|
2014-08-28 07:39:27 -04:00
|
|
|
if lock {
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
}
|
2014-08-11 14:07:37 -04:00
|
|
|
|
|
|
|
if container.Config.OpenStdin {
|
2014-08-26 18:44:00 -04:00
|
|
|
if err := container.stdin.Close(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("%s: Error close stdin: %s", container.ID, err)
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
if err := container.stdout.Clean(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("%s: Error close stdout: %s", container.ID, err)
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
if err := container.stderr.Clean(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("%s: Error close stderr: %s", container.ID, err)
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if container.command != nil && container.command.ProcessConfig.Terminal != nil {
|
|
|
|
if err := container.command.ProcessConfig.Terminal.Close(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("%s: Error closing terminal: %s", container.ID, err)
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-create a brand new stdin pipe once the container exited
|
|
|
|
if container.Config.OpenStdin {
|
2014-08-26 18:44:00 -04:00
|
|
|
container.stdin, container.stdinPipe = io.Pipe()
|
2014-08-11 14:07:37 -04:00
|
|
|
}
|
|
|
|
|
2015-02-04 14:04:58 -05:00
|
|
|
if container.logDriver != nil {
|
2015-03-18 14:44:14 -04:00
|
|
|
if container.logCopier != nil {
|
|
|
|
exit := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
container.logCopier.Wait()
|
|
|
|
close(exit)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Warnf("Logger didn't exit in time: logs may be truncated")
|
2015-03-18 14:44:14 -04:00
|
|
|
case <-exit:
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 14:04:58 -05:00
|
|
|
container.logDriver.Close()
|
2015-03-18 14:44:14 -04:00
|
|
|
container.logCopier = nil
|
2015-02-04 14:04:58 -05:00
|
|
|
container.logDriver = nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
c := container.command.ProcessConfig.Cmd
|
2014-08-11 14:07:37 -04:00
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
container.command.ProcessConfig.Cmd = exec.Cmd{
|
2014-08-11 14:07:37 -04:00
|
|
|
Stdin: c.Stdin,
|
|
|
|
Stdout: c.Stdout,
|
|
|
|
Stderr: c.Stderr,
|
|
|
|
Path: c.Path,
|
|
|
|
Env: c.Env,
|
|
|
|
ExtraFiles: c.ExtraFiles,
|
|
|
|
Args: c.Args,
|
|
|
|
Dir: c.Dir,
|
|
|
|
SysProcAttr: c.SysProcAttr,
|
|
|
|
}
|
|
|
|
}
|