github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/daemon/restart.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 6 "github.com/Sirupsen/logrus" 7 "github.com/docker/docker/container" 8 ) 9 10 // ContainerRestart stops and starts a container. It attempts to 11 // gracefully stop the container within the given timeout, forcefully 12 // stopping it if the timeout is exceeded. If given a negative 13 // timeout, ContainerRestart will wait forever until a graceful 14 // stop. Returns an error if the container cannot be found, or if 15 // there is an underlying error at any stage of the restart. 16 func (daemon *Daemon) ContainerRestart(name string, seconds int) error { 17 container, err := daemon.GetContainer(name) 18 if err != nil { 19 return err 20 } 21 if err := daemon.containerRestart(container, seconds); err != nil { 22 return fmt.Errorf("Cannot restart container %s: %v", name, err) 23 } 24 return nil 25 } 26 27 // containerRestart attempts to gracefully stop and then start the 28 // container. When stopping, wait for the given duration in seconds to 29 // gracefully stop, before forcefully terminating the container. If 30 // given a negative duration, wait forever for a graceful stop. 31 func (daemon *Daemon) containerRestart(container *container.Container, seconds int) error { 32 // Avoid unnecessarily unmounting and then directly mounting 33 // the container when the container stops and then starts 34 // again 35 if err := daemon.Mount(container); err == nil { 36 defer daemon.Unmount(container) 37 } 38 39 // set AutoRemove flag to false before stop so the container won't be 40 // removed during restart process 41 autoRemove := container.HostConfig.AutoRemove 42 43 container.HostConfig.AutoRemove = false 44 err := daemon.containerStop(container, seconds) 45 // restore AutoRemove irrespective of whether the stop worked or not 46 container.HostConfig.AutoRemove = autoRemove 47 // containerStop will write HostConfig to disk, we shall restore AutoRemove 48 // in disk too 49 if toDiskErr := container.ToDiskLocking(); toDiskErr != nil { 50 logrus.Errorf("Write container to disk error: %v", toDiskErr) 51 } 52 53 if err != nil { 54 return err 55 } 56 57 if err = daemon.containerStart(container); err != nil { 58 return err 59 } 60 61 daemon.LogContainerEvent(container, "restart") 62 return nil 63 }