I’m trying to add healthcheck for my docker app by grep the curl result.
When i run into docker container:
curl -sS http://nginx | grep -c works > /dev/null
echo $?
I getting 0 and everything seems fine, but if I’m trying to add it into docker-compose
version: "3.8"
services:
php:
build: .
volumes:
- ./www:/var/www
healthcheck:
test: ["CMD", "curl", "-sS", "http://nginx | grep", "-c", "works > /dev/null"]
interval: 5s
timeout: 5s
retries: 5
nginx:
image: "nginx:latest"
ports:
- "8080:80"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./www:/var/www
links:
- php
and check it by docker-compose ps
it has status "unhealthy". What am I doing wrong?
3
Answers
I guess your problem is the pipe operator. Afaik you also dont need the CMD.
You can try it like this.
You can also check what your health check is actually doing.
There are two problems here:
You split your arguments in a weird way –
"http://nginx | grep"
and"works > /dev/null"
don’t make much sense and will be passed literally tocurl
You are using a format in
test
which does not call the shell – as such, shell fetures like|
and>
won’t work since they need to be interpreted by your shell. You need to either passtest
as a string, or use theCMD-SHELL
form:Or:
For more information on
healthcheck
, you can read about it in the compose file reference.Healthcheck can be specified either in shell form, or as an EXEC array (like you have done).
The exec array doesn’t support most shell functionality, like using pipes.
I expect it will work fine if you specify it in shell form. I.e.
Or wrap it in
["CMD", "/bin/sh", "-c", "curl -sS ..."]
.