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