hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
30.98k stars 2.75k forks source link

wait for full initializing of postgres with hasura/graphql-engine image #6395

Open karpov-denys opened 3 years ago

karpov-denys commented 3 years ago

Dear community, I need some help. I stuck here. I use docker-compose for starting my services. Before work with DB I need add prod database dump to it. It is not quick operation. For that period DB is not available. For avoid errors from hasura/graphql-engine I have added restart: always. It is help to connect both container, but that had not been very informative for other developer. Also in the case of restart in the background we lose logs Is it possible to use pattern wait-for from docs.docker.com? I try to add it, but graphql-steady service is not initialized in correct way now. I do not now which command I should add to command: for proper start of hasura services inside graphql-steady.

...
  graphql-steady:
    image: hasura/graphql-engine:v1.3.1
    restart: always
    volumes:
      - ./GraphQLSteady/hasura/migrations:/hasura-migrations
      - ./GraphQLSteady/hasura/metadata:/hasura-metadata
      - ./MonolitDB/wait-for.sh:/wait-for.sh
    environment:
      HASURA_GRAPHQL_DATABASE_URL: postgres://${DB_SUPERUSERNAME}:${DB_PASS}@monolit-postgres:5432/${DB_NAME}
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" 
      HASURA_GRAPHQL_DEV_MODE: ${HASURA_GRAPHQL_DEV_MODE}
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      HASURA_GRAPHQL_MIGRATIONS_DIR: /hasura-migrations
      HASURA_GRAPHQL_METADATA_DIR: /hasura-metadata
    depends_on:
       - monolit-postgres
    ports:
      - 8090:8080
    command: sh -c "./wait-for monolit-postgres:5432 -- echo 'DB is ready'"

  monolit-postgres: 
    # has some delay before it is available. reason is importing database dump .
    build:
      context: ./MonolitDB
    ports:
      - 5441:5432
    volumes:
      - ./MonolitDB/dumps/${DB_DUMP}:/docker-entrypoint-initdb.d/1-dump.sql
      - ./GraphQLSteady/hasura_permissions.sql:/docker-entrypoint-initdb.d/2-hasura_permissions.sql
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: ${DB_SUPERUSERNAME}
      POSTGRES_PASSWORD: ${DB_PASS}
      POSTGRES_DB: ${DB_NAME}

...
tirumaraiselvan commented 3 years ago

I am not super familiar with "wait-for" but it seems like the database being ready (i.e. listening on port 5432) is not the important thing because it will be listening on port 5432 even during the db dump. The real waiting should be for the database to have a complete state.

The link you provided has an example shell script wait-for-postgres.sh: https://docs.docker.com/compose/startup-order/ and you can perhaps change the command there to "select 1" from a table which is supposed to mark as the database dump being finished?

karpov-denys commented 3 years ago

I am not super familiar with "wait-for" but it seems like the database being ready (i.e. listening on port 5432) is not the important thing because it will be listening on port 5432 even during the db dump. The real waiting should be for the database to have a complete state.

Yes, you are right. A database is ready, when all migrations and dump are added. It is mean database has complete state. If I'm not mistaken, in the period of running sql script, migration, etс. database does not response on 5432 port.

The link you provided has an example shell script wait-for-postgres.sh: https://docs.docker.com/compose/startup-order/ and you can perhaps change the command there to "select 1" from a table which is supposed to mark as the database dump being finished?

The problem is not in run command inside script. In case of adding CMD ... or command: ... in custom Dockerfile or docker-compose.yml hasura/graphql-engine is not initialized in correct way. I think line command: ... grind the correct command inside oficial hasura/graphql-engine Dockerfile

CuriousLearner commented 1 year ago

I'm trying to do something similar. Did you find a way around it @karpov-denys ?

willnode commented 4 months ago

For any who stumbles upon this issue on google search. I found using podman compose works rather than podman-compose.