diff --git a/README.md b/README.md index 224f158..8b3b260 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ docker run --rm v2tec/watchtower --help * `--interval, -i` Poll interval (in seconds). This value controls how frequently watchtower will poll for new images. Defaults to 300 seconds (5 minutes). * `--schedule, -s` [Cron expression](https://godoc.org/github.com/robfig/cron#hdr-CRON_Expression_Format) in 6 fields (rather than the traditional 5) which defines when and how often to check for new images. Either `--interval` or the schedule expression could be defined, but not both. An example: `--schedule "0 0 4 * * *" ` * `--no-pull` Do not pull new images. When this flag is specified, watchtower will not attempt to pull new images from the registry. Instead it will only monitor the local image cache for changes. Use this option if you are building new images directly on the Docker host without pushing them to a registry. +* `--stop-timeout` Timeout before the container is forcefully stopped. When set, this option will change the default (`10s`) wait time to the given value. An example: `--stop-timeout 30s` will set the timeout to 30 seconds. * `--label-enable` Watch containers where the `com.centurylinklabs.watchtower.enable` label is set to true. * `--cleanup` Remove old images after updating. When this flag is specified, watchtower will remove the old image after restarting a container with a new image. Use this option to prevent the accumulation of orphaned images on your system as containers are updated. * `--tlsverify` Use TLS when connecting to the Docker socket and verify the server's certificate. diff --git a/actions/update.go b/actions/update.go index 1f61af5..3866d4b 100644 --- a/actions/update.go +++ b/actions/update.go @@ -10,7 +10,6 @@ import ( var ( letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - waitTime = 10 * time.Second ) func allContainersFilter(container.Container) bool { return true } @@ -34,7 +33,7 @@ func containerFilter(names []string) container.Filter { // used to start those containers have been updated. If a change is detected in // any of the images, the associated containers are stopped and restarted with // the new image. -func Update(client container.Client, names []string, cleanup bool, noRestart bool) error { +func Update(client container.Client, names []string, cleanup bool, noRestart bool, timeout time.Duration) error { log.Debug("Checking containers for updated images") containers, err := client.ListContainers(containerFilter(names)) @@ -67,7 +66,7 @@ func Update(client container.Client, names []string, cleanup bool, noRestart boo } if container.Stale { - if err := client.StopContainer(container, waitTime); err != nil { + if err := client.StopContainer(container, timeout); err != nil { log.Error(err) } } diff --git a/main.go b/main.go index 962ab33..c84f26c 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ var ( cleanup bool noRestart bool notifier *notifications.Notifier + timeout time.Duration ) func init() { @@ -81,6 +82,12 @@ func main() { Usage: "use TLS and verify the remote", EnvVar: "DOCKER_TLS_VERIFY", }, + cli.DurationFlag{ + Name: "stop-timeout", + Usage: "timeout before container is forcefully stopped", + Value: time.Second * 10, + EnvVar: "WATCHTOWER_TIMEOUT", + }, cli.BoolFlag{ Name: "label-enable", Usage: "watch containers where the com.centurylinklabs.watchtower.enable label is true", @@ -178,6 +185,10 @@ func before(c *cli.Context) error { cleanup = c.GlobalBool("cleanup") noRestart = c.GlobalBool("no-restart") + timeout = c.GlobalDuration("stop-timeout") + if timeout < 0 { + log.Fatal("Please specify a positive value for timeout value.") + } // configure environment vars for client err := envConfig(c) @@ -209,7 +220,7 @@ func start(c *cli.Context) error { case v := <-tryLockSem: defer func() { tryLockSem <- v }() notifier.StartNotification() - if err := actions.Update(client, names, cleanup, noRestart); err != nil { + if err := actions.Update(client, names, cleanup, noRestart, timeout); err != nil { log.Println(err) } notifier.SendNotification()