github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/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  
    18  	if err := daemon.containerPause(container); err != nil {
    19  		return err
    20  	}
    21  
    22  	return nil
    23  }
    24  
    25  // containerPause pauses the container execution without stopping the process.
    26  // The execution can be resumed by calling containerUnpause.
    27  func (daemon *Daemon) containerPause(container *container.Container) error {
    28  	container.Lock()
    29  	defer container.Unlock()
    30  
    31  	// We cannot Pause the container which is not running
    32  	if !container.Running {
    33  		return errNotRunning(container.ID)
    34  	}
    35  
    36  	// We cannot Pause the container which is already paused
    37  	if container.Paused {
    38  		return errNotPaused(container.ID)
    39  	}
    40  
    41  	// We cannot Pause the container which is restarting
    42  	if container.Restarting {
    43  		return errContainerIsRestarting(container.ID)
    44  	}
    45  
    46  	if err := daemon.containerd.Pause(context.Background(), container.ID); err != nil {
    47  		return fmt.Errorf("Cannot pause container %s: %s", container.ID, err)
    48  	}
    49  
    50  	container.Paused = true
    51  	daemon.setStateCounter(container)
    52  	daemon.updateHealthMonitor(container)
    53  	daemon.LogContainerEvent(container, "pause")
    54  
    55  	if err := container.CheckpointTo(daemon.containersReplica); err != nil {
    56  		logrus.WithError(err).Warn("could not save container to disk")
    57  	}
    58  
    59  	return nil
    60  }