github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/daemon/pause.go (about)

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