docker_practice/image/dockerfile/healthcheck.md

84 lines
4.9 KiB
Go
Raw Normal View History

### HEALTHCHECK
* `HEALTHCHECK [选项] CMD <命令>`
* `HEALTHCHECK NONE`使
`HEALTHCHECK` Docker Docker 1.12
`HEALTHCHECK` Docker 退退 1.12 Docker
1.12 Docker `HEALTHCHECK`
`HEALTHCHECK` `starting` `HEALTHCHECK` `healthy` `unhealthy`
`HEALTHCHECK`
2016-12-15 09:42:50 +08:00
* `--interval=<间隔>` 30
* `--timeout=<时长>` 30
* `--retries=<次数>` `unhealthy` 3
`CMD`, `ENTRYPOINT` `HEALTHCHECK`
`HEALTHCHECK [选项] CMD` `ENTRYPOINT` `shell` `exec` `0``1``2`使
Web Web `curl` `Dockerfile` `HEALTHCHECK`
```docker
FROM nginx
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
HEALTHCHECK --interval=5s --timeout=3s \
CMD curl -fs http://localhost/ || exit 1
```
5 3 使 `curl -fs http://localhost/ || exit 1`
使 `docker build`
```bash
$ docker build -t myweb:v1 .
```
```bash
$ docker run -d --name web -p 80:80 myweb:v1
```
`docker container ls` `(health: starting)`
```bash
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03e28eb00bd0 myweb:v1 "nginx -g 'daemon off" 3 seconds ago Up 2 seconds (health: starting) 80/tcp, 443/tcp web
```
`docker container ls` `(healthy)`
```bash
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03e28eb00bd0 myweb:v1 "nginx -g 'daemon off" 18 seconds ago Up 16 seconds (healthy) 80/tcp, 443/tcp web
```
`(unhealthy)`
`stdout` `stderr` `docker inspect`
```bash
$ docker inspect --format '{{json .State.Health}}' web | python -m json.tool
{
"FailingStreak": 0,
"Log": [
{
"End": "2016-11-25T14:35:37.940957051Z",
"ExitCode": 0,
"Output": "<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to nginx!</title>\n<style>\n body {\n width: 35em;\n margin: 0 auto;\n font-family: Tahoma, Verdana, Arial, sans-serif;\n }\n</style>\n</head>\n<body>\n<h1>Welcome to nginx!</h1>\n<p>If you see this page, the nginx web server is successfully installed and\nworking. Further configuration is required.</p>\n\n<p>For online documentation and support please refer to\n<a href=\"http://nginx.org/\">nginx.org</a>.<br/>\nCommercial support is available at\n<a href=\"http://nginx.com/\">nginx.com</a>.</p>\n\n<p><em>Thank you for using nginx.</em></p>\n</body>\n</html>\n",
"Start": "2016-11-25T14:35:37.780192565Z"
}
],
"Status": "healthy"
}
```