If I understand correctly, and based on the examples out there, the healthcheck command keeps the flag "starting" until the flag returns code 0, then it becomes "healthy". I'm unable to reach that state with this simple Dockerfile:
FROM ubuntu:latest
# find "sleep infinity" in ps -ax, and use as a way to decide that the execution finished
HEALTHCHECK --start-period=30s --interval=30s --timeout=4s --retries=1000 CMD [ "$(ps -ax | grep -i "sleep infinity" | grep -v grep | wc -l)" == 2 ] || exit 1
CMD echo "Hello, we're done with what we're supposed to do" && sleep infinity
The purpose of this Dockerfile is to block ad-infinitum once all the processes required finish successfully. The healthcheck will be used in docker-compose to order the container execution. In practice, this is just an updater that has to run first before anything else.
By building and running the container:
$ docker build . -t mytest
$ docker run --name mytest-cont mytest
Then we can run in another terminal
$ watch docker ps
and we'll see that the state "health: starting" never changes.
The weird part is that the command used for health check works fine. If we enter the container:
$ docker exec -it mytest-cont /bin/bash
Then run that command, as copy pasted, then check the exit code, it returns zero:
$ [ "$(ps -ax | grep -i "sleep infinity" | grep -v grep | wc -l)" == 2 ]
$ echo $?
where we see the returned value is zero!
So... the command works in bash. Why is it not working as expected in docker and being accepted as healthcheck command to report the health of the container in docker ps
?
CodePudding user response:
CMD
is running commands with sh
, not bash
.
So you need to do = 2
instead of == 2