github.com/rawahars/moby@v24.0.4+incompatible/daemon/pause.go (about)

     1  package daemon // import "github.com/docker/docker/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  	ctr, err := daemon.GetContainer(name)
    14  	if err != nil {
    15  		return err
    16  	}
    17  	return daemon.containerPause(ctr)
    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  	tsk, err := container.GetRunningTask()
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	// We cannot Pause the container which is already paused
    33  	if container.Paused {
    34  		return errNotPaused(container.ID)
    35  	}
    36  
    37  	// We cannot Pause the container which is restarting
    38  	if container.Restarting {
    39  		return errContainerIsRestarting(container.ID)
    40  	}
    41  
    42  	if err := tsk.Pause(context.Background()); err != nil {
    43  		return fmt.Errorf("cannot pause container %s: %s", container.ID, err)
    44  	}
    45  
    46  	container.Paused = true
    47  	daemon.setStateCounter(container)
    48  	daemon.updateHealthMonitor(container)
    49  	daemon.LogContainerEvent(container, "pause")
    50  
    51  	if err := container.CheckpointTo(daemon.containersReplica); err != nil {
    52  		logrus.WithError(err).Warn("could not save container to disk")
    53  	}
    54  
    55  	return nil
    56  }