postgis / docker-postgis

Docker image for PostGIS
https://hub.docker.com/r/postgis/postgis/
MIT License
1.35k stars 462 forks source link

Detect when container is ready #316

Open latot opened 1 year ago

latot commented 1 year ago

Hi all, I'm trying to use postgis for apps, but is hard because I can't found a way to know when a contianer is ready (I can need mount several of them).

Is not like there is no way to check the logs with regex, but I think would be great have a way to can now when the container is ready.

There is a important thing about this type of container, I checked https://github.com/docker-library/postgres/issues/146, sadly does not has a cross-platform solution, pg_isready helps in the postgres case, but in case of postgis not, because after postgres is mounted, then the extensions must be installed and then the server restarts, due to this pg_ready ends in the installation of the extension because is a moment where postgres is working.

There can be different solutions, maybe a cross-platform way, is to have a file in the root of the container with the a general status of the container, where checking it we can now when the container is ready, maybe someone else have a robust solution to that.

Thx!

ImreSamu commented 1 year ago

Hi latot,

... and then the server restarts,

As I understand, the ~"temporary daemon" is running only on the Unix socket.

And there is ~ good documentation about the initialization steps used by this project.

pg_isready

so if you don't want to connect to the ~"temporary daemon" ( via Unix socket. ) try the opposite and ADD the hostname!

until pg_isready -h $(hostname -i); do
  sleep 0.1
done

( as suggested in https://github.com/docker-library/postgres/issues/146#issuecomment-659694449 )

latot commented 1 year ago

Hi!, pg_isready does not works very well in postgis (in postgres works), because when postgis is installing the extesion, pg_isready tells the server is ready. After the installation the server is restarted, so wait pg_isready is not very good here D:

ImreSamu commented 1 year ago

Hi!, pg_isready does not works very well in postgis

If I understand you correctly, you re-tested pg_isready with the --host parameter and it did not solve your problem. ouch ... In that case, please provide a minimal working example that I can check.

ImreSamu commented 1 year ago

my suggestion is the same as the upstream suggestion

via https://github.com/docker-library/postgres/issues/146#issuecomment-482394709

latot commented 1 year ago

Hi, I think I don't understand you exaplain in your first comment, now I get it, I'll test probable monday, thx for your help :)

latot commented 1 year ago

Hi!, well, first sorry, my redaction was very ugly in the last comment.

And yes, the method works!, I think can be easy have it in the docs more than in the comments, in order to do this works in cross platform, I'm doing the next thing, first instead run the complete sh script, only run pg_isready and get the timer over the language we are using, for now I'm using R, but this is applicable to any language:

while (TRUE){
    Sys.sleep(5)
    if (
        processx::run(
            "docker",
            c(
                "exec",
                postgres_docker_id,
                "pg_isready",
                "-h", "localhost"
            ),
            error_on_status = FALSE
        )$status == 0
    ) break
}

I think the last thing, is ideally update the docs and specify how to use pg_isready.

Thx!