github.com/rumpl/bof@v23.0.0-rc.2+incompatible/daemon/restart.go (about)

     1  package daemon // import "github.com/docker/docker/daemon"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	containertypes "github.com/docker/docker/api/types/container"
     8  	"github.com/docker/docker/container"
     9  )
    10  
    11  // ContainerRestart stops and starts a container. It attempts to
    12  // gracefully stop the container within the given timeout, forcefully
    13  // stopping it if the timeout is exceeded. If given a negative
    14  // timeout, ContainerRestart will wait forever until a graceful
    15  // stop. Returns an error if the container cannot be found, or if
    16  // there is an underlying error at any stage of the restart.
    17  func (daemon *Daemon) ContainerRestart(ctx context.Context, name string, options containertypes.StopOptions) error {
    18  	ctr, err := daemon.GetContainer(name)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	err = daemon.containerRestart(ctx, ctr, options)
    23  	if err != nil {
    24  		return fmt.Errorf("Cannot restart container %s: %v", name, err)
    25  	}
    26  	return nil
    27  }
    28  
    29  // containerRestart attempts to gracefully stop and then start the
    30  // container. When stopping, wait for the given duration in seconds to
    31  // gracefully stop, before forcefully terminating the container. If
    32  // given a negative duration, wait forever for a graceful stop.
    33  func (daemon *Daemon) containerRestart(ctx context.Context, container *container.Container, options containertypes.StopOptions) error {
    34  	// Determine isolation. If not specified in the hostconfig, use daemon default.
    35  	actualIsolation := container.HostConfig.Isolation
    36  	if containertypes.Isolation.IsDefault(actualIsolation) {
    37  		actualIsolation = daemon.defaultIsolation
    38  	}
    39  
    40  	// Avoid unnecessarily unmounting and then directly mounting
    41  	// the container when the container stops and then starts
    42  	// again. We do not do this for Hyper-V isolated containers
    43  	// (implying also on Windows) as the HCS must have exclusive
    44  	// access to mount the containers filesystem inside the utility
    45  	// VM.
    46  	if !containertypes.Isolation.IsHyperV(actualIsolation) {
    47  		if err := daemon.Mount(container); err == nil {
    48  			defer daemon.Unmount(container)
    49  		}
    50  	}
    51  
    52  	if container.IsRunning() {
    53  		container.Lock()
    54  		container.HasBeenManuallyRestarted = true
    55  		container.Unlock()
    56  
    57  		err := daemon.containerStop(ctx, container, options)
    58  
    59  		if err != nil {
    60  			return err
    61  		}
    62  	}
    63  
    64  	if err := daemon.containerStart(container, "", "", true); err != nil {
    65  		return err
    66  	}
    67  
    68  	daemon.LogContainerEvent(container, "restart")
    69  	return nil
    70  }