Skip to content

Commit afb2c8b

Browse files
authored
Merge pull request #117 from infra7ti/upstream
Monitor only running containers and better writing of README.md
2 parents 31a64b4 + 8203514 commit afb2c8b

2 files changed

Lines changed: 88 additions & 31 deletions

File tree

README.md

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ This container is a stand-in till there is native support for `--exit-on-unhealt
99
- [`1.1.0` (*Dockerfile*)](https://github.com/willfarrell/docker-autoheal/blob/1.1.0/Dockerfile)
1010
- [`v0.7.0` (*Dockerfile*)](https://github.com/willfarrell/docker-autoheal/blob/v0.7.0/Dockerfile)
1111

12+
1213
![](https://img.shields.io/docker/pulls/willfarrell/autoheal "Total docker pulls") [![](https://images.microbadger.com/badges/image/willfarrell/autoheal.svg)](http://microbadger.com/images/willfarrell/autoheal "Docker layer breakdown")
1314

1415
## How to use
15-
### UNIX socket passthrough
16+
17+
### 1. Docker CLI
18+
#### UNIX socket passthrough
1619
```bash
1720
docker run -d \
1821
--name autoheal \
@@ -21,59 +24,99 @@ docker run -d \
2124
-v /var/run/docker.sock:/var/run/docker.sock \
2225
willfarrell/autoheal
2326
```
24-
### TCP socket
27+
#### TCP socket
2528
```bash
2629
docker run -d \
2730
--name autoheal \
2831
--restart=always \
2932
-e AUTOHEAL_CONTAINER_LABEL=all \
30-
-e DOCKER_SOCK=tcp://HOST:PORT \
33+
-e DOCKER_SOCK=tcp://$HOST:$PORT \
3134
-v /path/to/certs/:/certs/:ro \
3235
willfarrell/autoheal
3336
```
34-
a) Apply the label `autoheal=true` to your container to have it watched.
35-
36-
b) Set ENV `AUTOHEAL_CONTAINER_LABEL=all` to watch all running containers.
37-
38-
c) Set ENV `AUTOHEAL_CONTAINER_LABEL` to existing label name that has the value `true`.
39-
40-
Note: You must apply `HEALTHCHECK` to your docker images first. See https://docs.docker.com/engine/reference/builder/#healthcheck for details.
41-
See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS
42-
43-
The certificates, and keys need these names:
37+
#### TCP with mTLS (HTTPS)
38+
```bash
39+
docker run -d \
40+
--name autoheal \
41+
--restart=always \
42+
--tlscacert=/certs/ca.pem \
43+
--tlscert=/certs/client-cert.pem \
44+
--tlskey=/certs/client-key.pem \
45+
-e AUTOHEAL_CONTAINER_LABEL=all \
46+
-e DOCKER_HOST=tcp://$HOST:2376 \
47+
-e DOCKER_SOCK=tcps://$HOST:2376 \
48+
-e DOCKER_TLS_VERIFY=1 \
49+
-v /path/to/certs/:/certs/:ro \
50+
willfarrell/autoheal
51+
```
52+
The certificates and keys need these names and resides under /certs inside the container:
4453
* ca.pem
4554
* client-cert.pem
4655
* client-key.pem
4756

57+
> See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS
58+
4859
### Change Timezone
4960
If you need the timezone to match the local machine, you can map the `/etc/localtime` into the container.
50-
```
61+
```bash
5162
docker run ... -v /etc/localtime:/etc/localtime:ro
5263
```
5364

65+
### 2. Use in your container image
66+
Choose one of the three alternatives:
5467

55-
## ENV Defaults
56-
```
57-
AUTOHEAL_CONTAINER_LABEL=autoheal
58-
AUTOHEAL_INTERVAL=5 # check every 5 seconds
59-
AUTOHEAL_START_PERIOD=0 # wait 0 seconds before first health check
60-
AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 # Docker waits max 10 seconds (the Docker default) for a container to stop before killing during restarts (container overridable via label, see below)
61-
DOCKER_SOCK=/var/run/docker.sock # Unix socket for curl requests to Docker API
62-
CURL_TIMEOUT=30 # --max-time seconds for curl requests to Docker API
63-
WEBHOOK_URL="" # post message to the webhook if a container was restarted (or restart failed)
64-
```
68+
a) Apply the label `autoheal=true` to your container to have it watched;<br/>
69+
b) Set ENV `AUTOHEAL_CONTAINER_LABEL=all` to watch all running containers;<br/>
70+
c) Set ENV `AUTOHEAL_CONTAINER_LABEL` to existing container label that has the value `true`;<br/>
6571

66-
### Optional Container Labels
67-
```
68-
autoheal.stop.timeout=20 # Per containers override for stop timeout seconds during restart
72+
> Note: You must apply `HEALTHCHECK` to your docker images first.<br/>
73+
> See https://docs.docker.com/engine/reference/builder/#healthcheck for details.
74+
75+
#### Docker Compose (example)
76+
```yaml
77+
services:
78+
app:
79+
extends:
80+
file: ${PWD}/services.yml
81+
service: app
82+
labels:
83+
autoheal-app: true
84+
85+
autoheal:
86+
deploy:
87+
replicas: 1
88+
environment:
89+
AUTOHEAL_CONTAINER_LABEL: autoheal-app
90+
image: willfarrell/autoheal:latest
91+
network_mode: none
92+
restart: always
93+
volumes:
94+
- /etc/localtime:/etc/localtime:ro
95+
- /var/run/docker.sock:/var/run/docker.sock
6996
```
7097
71-
## Testing
98+
#### Optional Container Labels
99+
|`autoheal.stop.timeout=20` |Per containers override for stop timeout seconds during restart|
100+
| --- | --- |
101+
102+
## Environment Defaults
103+
|Variable |Description|
104+
| --- | --- |
105+
|`AUTOHEAL_CONTAINER_LABEL=autoheal` |set to existing label name that has the value `true`|
106+
|`AUTOHEAL_INTERVAL=5` |check every 5 seconds|
107+
|`AUTOHEAL_START_PERIOD=0` |wait 0 seconds before first health check|
108+
|`AUTOHEAL_DEFAULT_STOP_TIMEOUT=10` |Docker waits max 10 seconds (the Docker default) for a container to stop before killing during restarts (container overridable via label, see below)|
109+
|`AUTOHEAL_ONLY_MONITOR_RUNNING=false` |All containers monitored by default. Set this to true to only monitor running containers. This will result in Paused contaners being ignored.|
110+
|`DOCKER_SOCK=/var/run/docker.sock` |Unix socket for curl requests to Docker API|
111+
|`CURL_TIMEOUT=30` |--max-time seconds for curl requests to Docker API|
112+
|`WEBHOOK_URL=""` |post message to the webhook if a container was restarted (or restart failed)|
113+
114+
## Testing (building locally)
72115
```bash
73-
docker build -t autoheal .
116+
docker buildx build -t autoheal .
74117
75118
docker run -d \
76119
-e AUTOHEAL_CONTAINER_LABEL=all \
77120
-v /var/run/docker.sock:/var/run/docker.sock \
78-
autoheal
121+
autoheal
79122
```

docker-entrypoint

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ AUTOHEAL_CONTAINER_LABEL=${AUTOHEAL_CONTAINER_LABEL:-autoheal}
2929
AUTOHEAL_START_PERIOD=${AUTOHEAL_START_PERIOD:-0}
3030
AUTOHEAL_INTERVAL=${AUTOHEAL_INTERVAL:-5}
3131
AUTOHEAL_DEFAULT_STOP_TIMEOUT=${AUTOHEAL_DEFAULT_STOP_TIMEOUT:-10}
32+
AUTOHEAL_ONLY_MONITOR_RUNNING=${AUTOHEAL_ONLY_MONITOR_RUNNING:-false}
33+
34+
echo AUTOHEAL_CONTAINER_LABEL=${AUTOHEAL_CONTAINER_LABEL}
35+
echo AUTOHEAL_START_PERIOD=${AUTOHEAL_START_PERIOD}
36+
echo AUTOHEAL_INTERVAL=${AUTOHEAL_INTERVAL}
37+
echo AUTOHEAL_DEFAULT_STOP_TIMEOUT=${AUTOHEAL_DEFAULT_STOP_TIMEOUT}
38+
echo AUTOHEAL_ONLY_MONITOR_RUNNING=${AUTOHEAL_ONLY_MONITOR_RUNNING}
3239

3340
docker_curl() {
3441
curl --max-time "${CURL_TIMEOUT}" --no-buffer -s \
@@ -40,6 +47,7 @@ docker_curl() {
4047
# shellcheck disable=2039
4148
get_container_info() {
4249
local label_filter
50+
local running_filter
4351
local url
4452

4553
# Set container selector
@@ -49,7 +57,13 @@ get_container_info() {
4957
else
5058
label_filter=",\"label\":\[\"${AUTOHEAL_CONTAINER_LABEL}=true\"\]"
5159
fi
52-
url="${HTTP_ENDPOINT}/containers/json?filters=\{\"health\":\[\"unhealthy\"\]${label_filter}\}"
60+
if [ "$AUTOHEAL_ONLY_MONITOR_RUNNING" = false ]
61+
then
62+
running_filter=""
63+
else
64+
running_filter=",\"status\":\[\"running\"\]"
65+
fi
66+
url="${HTTP_ENDPOINT}/containers/json?filters=\{\"health\":\[\"unhealthy\"\]${label_filter}${running_filter}\}"
5367
docker_curl "$url"
5468
}
5569

0 commit comments

Comments
 (0)