docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.14k stars 1.11k forks source link

add Healthcheck #1146

Closed ygerlach closed 7 months ago

ygerlach commented 8 months ago

This allows to check for the health of the postgres container. This is usefull for docker-compose. Services might use:

depends_on:
  mypostgres-container:
    condition: service_healthy

to check for a up and running postgres instance

normaly i would check for the open tcp port, but postgres starts the temporary server to setup the db. Those might cause a simple port check (like grep -q ":1538" /proc/net/tcp ) to report a false positive. This might also not be a guarante, that the server is available. Maybe it would be a good idea to check for the shm and the port like this:

HEALTHCHECK --interval=1s --timeout=1s --retries=60 \
    CMD-SHELL test -f /dev/shm/ready && grep -q :1538 /proc/net/tcp

Let me know what you think. Just a simple "process is running" check, or an advanced "process is running and post is open" check or an even more advanced "process is running and a SELECT 1; query works"

I just implemented it for one Dockerfile now. If you are fine with this, i would add this to every Dockerfile.

LaurentGoderre commented 8 months ago

We have a PR open for healthcheck with postgres. I am wondering if it would solve your issue: https://github.com/docker-library/docs/pull/2393/files

ygerlach commented 8 months ago

That looks promising, i havent tried it yet, but it looks, like that could (like a simple port check) also trigger during the startup phase of the container, when the server is started temporary for db setup. That could cause problems with depending services. Maybe a combination of the shm file to mark, that the server is started for real and pg_isready to make sure the server accepts connections would work. I am going to try that.

tianon commented 7 months ago

See also https://github.com/docker-library/faq#healthcheck (https://github.com/docker-library/healthcheck/tree/master/postgres)