github.com/akerouanton/docker@v1.11.0-rc3/daemon/restart.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/container"
     7  )
     8  
     9  // ContainerRestart stops and starts a container. It attempts to
    10  // gracefully stop the container within the given timeout, forcefully
    11  // stopping it if the timeout is exceeded. If given a negative
    12  // timeout, ContainerRestart will wait forever until a graceful
    13  // stop. Returns an error if the container cannot be found, or if
    14  // there is an underlying error at any stage of the restart.
    15  func (daemon *Daemon) ContainerRestart(name string, seconds int) error {
    16  	container, err := daemon.GetContainer(name)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	if err := daemon.containerRestart(container, seconds); err != nil {
    21  		return fmt.Errorf("Cannot restart container %s: %v", name, err)
    22  	}
    23  	return nil
    24  }
    25  
    26  // containerRestart attempts to gracefully stop and then start the
    27  // container. When stopping, wait for the given duration in seconds to
    28  // gracefully stop, before forcefully terminating the container. If
    29  // given a negative duration, wait forever for a graceful stop.
    30  func (daemon *Daemon) containerRestart(container *container.Container, seconds int) error {
    31  	// Avoid unnecessarily unmounting and then directly mounting
    32  	// the container when the container stops and then starts
    33  	// again
    34  	if err := daemon.Mount(container); err == nil {
    35  		defer daemon.Unmount(container)
    36  	}
    37  
    38  	if err := daemon.containerStop(container, seconds); err != nil {
    39  		return err
    40  	}
    41  
    42  	if err := daemon.containerStart(container); err != nil {
    43  		return err
    44  	}
    45  
    46  	daemon.LogContainerEvent(container, "restart")
    47  	return nil
    48  }