watchtower/docs/lifecycle-hooks.md

52 lines
2.3 KiB
Markdown
Raw Normal View History

## Executing commands before and after updating
2019-08-26 16:44:10 +02:00
> **DO NOTE**: These are shell commands executed with `sh`, and therefore require the
> container to provide the `sh` executable.
2019-08-26 16:44:10 +02:00
It is possible to execute _pre/post\-check_ and _pre/post\-update_ commands
**inside** every container updated by watchtower.
- The _pre-check_ command is executed before checking the container for updates.
- The _pre-update_ command is executed before stopping the container when an update is about to start.
- The _post-update_ command is executed after restarting the updated container
- The _post-check_ command is executed last after a updated container is started or no update was needed.
This feature is disabled by default. To enable it, you need to set the option
`--enable-lifecycle-hooks` on the command line, or set the environment variable
`WATCHTOWER_LIFECYCLE_HOOKS` to `true`.
### Specifying update commands
2019-08-26 16:44:10 +02:00
The commands are specified using docker container labels, the following are currently available:
- `com.centurylinklabs.watchtower.lifecycle.pre-check` - _pre-check_
- `com.centurylinklabs.watchtower.lifecycle.pre-update-command` - _pre-update_
- `com.centurylinklabs.watchtower.lifecycle.post-update` - _post-update_
- `com.centurylinklabs.watchtower.lifecycle.post-check` - _post-check_
2019-08-26 16:44:10 +02:00
These labels can be declared as instructions in a Dockerfile (with some example .sh files):
```docker
2019-08-26 16:44:10 +02:00
LABEL com.centurylinklabs.watchtower.lifecycle.pre-check="/sync.sh"
LABEL com.centurylinklabs.watchtower.lifecycle.pre-update="/dump-data.sh"
LABEL com.centurylinklabs.watchtower.lifecycle.post-update="/restore-data.sh"
2019-08-26 16:44:10 +02:00
LABEL com.centurylinklabs.watchtower.lifecycle.post-check="/send-heartbeat.sh"
```
Or be specified as part of the `docker run` command line:
```bash
docker run -d \
2019-08-26 16:44:10 +02:00
--label=com.centurylinklabs.watchtower.lifecycle.pre-check="/sync.sh" \
--label=com.centurylinklabs.watchtower.lifecycle.pre-update="/dump-data.sh" \
--label=com.centurylinklabs.watchtower.lifecycle.post-update="/restore-data.sh" \
someimage
2019-08-26 16:44:10 +02:00
--label=com.centurylinklabs.watchtower.lifecycle.post-check="/send-heartbeat.sh" \
```
### Execution failure
2019-08-26 16:44:10 +02:00
The failure of a command to execute, identified by an exit code different than
0, will not prevent watchtower from updating the container. Only an error
2019-08-26 16:44:10 +02:00
log statement containing the exit code will be reported.