Skip to content

Commit

Permalink
feat: add --health-check command line switch (#1725)
Browse files Browse the repository at this point in the history
Co-authored-by: nils måsén <[email protected]>
  • Loading branch information
bugficks and piksel committed Sep 16, 2023
1 parent 79ebad0 commit 8e3bde7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ func Run(c *cobra.Command, names []string) {
enableMetricsAPI, _ := c.PersistentFlags().GetBool("http-api-metrics")
unblockHTTPAPI, _ := c.PersistentFlags().GetBool("http-api-periodic-polls")
apiToken, _ := c.PersistentFlags().GetString("http-api-token")
healthCheck, _ := c.PersistentFlags().GetBool("health-check")

if healthCheck {
// health check should not have pid 1
if os.Getpid() == 1 {
time.Sleep(1 * time.Second)
log.Fatal("The health check flag should never be passed to the main watchtower container process")
}
os.Exit(0)
}

if rollingRestart && monitorOnly {
log.Fatal("Rolling restarts is not compatible with the global monitor only flag")
Expand Down
3 changes: 3 additions & 0 deletions dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ COPY --from=alpine \
EXPOSE 8080

COPY watchtower /

HEALTHCHECK CMD [ "/watchtower", "--health-check"]

ENTRYPOINT ["/watchtower"]
2 changes: 2 additions & 0 deletions dockerfiles/Dockerfile.dev-self-contained
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certifi
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /watchtower/watchtower /watchtower

HEALTHCHECK CMD [ "/watchtower", "--health-check"]

ENTRYPOINT ["/watchtower"]
2 changes: 2 additions & 0 deletions dockerfiles/Dockerfile.self-contained
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certifi
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /go/watchtower/watchtower /watchtower

HEALTHCHECK CMD [ "/watchtower", "--health-check"]

ENTRYPOINT ["/watchtower"]
11 changes: 11 additions & 0 deletions docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ Environment Variable: WATCHTOWER_WARN_ON_HEAD_FAILURE
Default: auto
```

## Health check

Returns a success exit code to enable usage with docker `HEALTHCHECK`. This check is naive and only returns checks whether there is another process running inside the container, as it is the only known form of failure state for watchtowers container.

!!! note "Only for HEALTHCHECK use"
Never put this on the main container executable command line as it is only meant to be run from docker HEALTHCHECK.

```text
Argument: --health-check
```

## Programatic Output (porcelain)

Writes the session results to STDOUT using a stable, machine-readable format (indicated by the argument VERSION).
Expand Down
8 changes: 7 additions & 1 deletion internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
"The maximum log level that will be written to STDERR. Possible values: panic, fatal, error, warn, info, debug or trace")

flags.BoolP(
"health-check",
"",
false,
"Do health check and exit")

flags.BoolP(
"label-take-precedence",
"",
viper.GetBool("WATCHTOWER_LABEL_TAKE_PRECEDENCE"),
envBool("WATCHTOWER_LABEL_TAKE_PRECEDENCE"),
"Label applied to containers take precedence over arguments")
}

Expand Down

0 comments on commit 8e3bde7

Please sign in to comment.