| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | ## Executing commands before and after updating
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 11:34:24 +02:00
										 |  |  | !!! note  | 
					
						
							|  |  |  |     These are shell commands executed with `sh`, and therefore require the container to provide the `sh` | 
					
						
							|  |  |  |     executable. | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-15 16:58:46 +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 for each container prior to every update cycle. | 
					
						
							|  |  |  | - 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 for each container post every update cycle. | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2019-08-09 16:38:46 +02:00
										 |  |  | `WATCHTOWER_LIFECYCLE_HOOKS` to `true`. | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Specifying update commands
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-15 16:58:46 +02:00
										 |  |  | The commands are specified using docker container labels, the following are currently available: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | | Type        | Docker Container Label                                 | | 
					
						
							|  |  |  | | ----------- | ------------------------------------------------------ |  | 
					
						
							|  |  |  | | Pre Check   | `com.centurylinklabs.watchtower.lifecycle.pre-check`   | | 
					
						
							|  |  |  | | Pre Update  | `com.centurylinklabs.watchtower.lifecycle.pre-update`  |  | 
					
						
							|  |  |  | | Post Update | `com.centurylinklabs.watchtower.lifecycle.post-update` | | 
					
						
							|  |  |  | | Post Check  | `com.centurylinklabs.watchtower.lifecycle.post-check`  | | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 11:34:24 +02:00
										 |  |  | These labels can be declared as instructions in a Dockerfile (with some example .sh files) or be specified as part of | 
					
						
							|  |  |  | the `docker run` command line: | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 11:34:24 +02:00
										 |  |  | === "Dockerfile" | 
					
						
							| 
									
										
										
										
											2021-04-27 17:44:49 +02:00
										 |  |  |     ```docker  | 
					
						
							|  |  |  |     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" | 
					
						
							|  |  |  |     LABEL com.centurylinklabs.watchtower.lifecycle.post-check="/send-heartbeat.sh" | 
					
						
							|  |  |  |     ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 11:34:24 +02:00
										 |  |  | === "docker run" | 
					
						
							| 
									
										
										
										
											2021-04-27 17:44:49 +02:00
										 |  |  |     ```bash  | 
					
						
							|  |  |  |     docker run -d \ | 
					
						
							|  |  |  |     --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 --label=com.centurylinklabs.watchtower.lifecycle.post-check="/send-heartbeat.sh" \ | 
					
						
							|  |  |  |     ``` | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 21:11:12 +01:00
										 |  |  | ### Timeouts
 | 
					
						
							|  |  |  | The timeout for all lifecycle commands is 60 seconds. After that, a timeout will | 
					
						
							|  |  |  | occur, forcing Watchtower to continue the update loop. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### Pre-update timeouts
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For the `pre-update` lifecycle command, it is possible to override this timeout to | 
					
						
							|  |  |  | allow the script to finish before forcefully killing it. This is done by adding the | 
					
						
							|  |  |  | label `com.centurylinklabs.watchtower.lifecycle.pre-update-timeout` followed by | 
					
						
							|  |  |  | the timeout expressed in minutes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the label value is explicitly set to `0`, the timeout will be disabled.   | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | ### Execution failure
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-15 16:58:46 +02:00
										 |  |  | The failure of a command to execute, identified by an exit code different than | 
					
						
							| 
									
										
										
										
											2019-07-27 01:37:16 +02:00
										 |  |  | 0, will not prevent watchtower from updating the container. Only an error | 
					
						
							| 
									
										
										
										
											2019-09-15 16:58:46 +02:00
										 |  |  | log statement containing the exit code will be reported. |