github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/daemon/pause.go (about)

     1  package daemon
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/docker/container"
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  // ContainerPause pauses a container
    12  func (daemon *Daemon) ContainerPause(name string) error {
    13  	container, err := daemon.GetContainer(name)
    14  	if err != nil {
    15  		return err
    16  	}
    17  	return daemon.containerPause(container)
    18  }
    19  
    20  // containerPause pauses the container execution without stopping the process.
    21  // The execution can be resumed by calling containerUnpause.
    22  func (daemon *Daemon) containerPause(container *container.Container) error {
    23  	container.Lock()
    24  	defer container.Unlock()
    25  
    26  	// We cannot Pause the container which is not running
    27  	if !container.Running {
    28  		return errNotRunning(container.ID)
    29  	}
    30  
    31  	// We cannot Pause the container which is already paused
    32  	if container.Paused {
    33  		return errNotPaused(container.ID)
    34  	}
    35  
    36  	// We cannot Pause the container which is restarting
    37  	if container.Restarting {
    38  		return errContainerIsRestarting(container.ID)
    39  	}
    40  
    41  	if err := daemon.containerd.Pause(context.Background(), container.ID); err != nil {
    42  		return fmt.Errorf("Cannot pause container %s: %s", container.ID, err)
    43  	}
    44  
    45  	container.Paused = true
    46  	daemon.setStateCounter(container)
    47  	daemon.updateHealthMonitor(container)
    48  	daemon.LogContainerEvent(container, "pause")
    49  
    50  	if err := container.CheckpointTo(daemon.containersReplica); err != nil {
    51  		logrus.WithError(err).Warn("could not save container to disk")
    52  	}
    53  
    54  	return nil
    55  }