Search code examples
dockergraphqlhasuragraphql-codegen

Make one Docker image wait for another to finish before building


I am very new to docker (apologies in advance for errors in my terminology / gaps in my knowledge) and have three services where one depends on one to finish before it can be built.

The repo is set up to have them both as submodules with the below docker file composing them.

version: "3"
services:
  db:
    image: postgres:12.3
    restart: always
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_INITDB_ARGS: "--data-checksums"
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
  actions:
    build:
      context: ./action-handlers
      dockerfile: Dockerfile.stg
    depends_on:
      - hasura
    environment:
      HASURA_GRAPHQL_ENDPOINT: http://hasura:8080/v1/graphql
      HASURA_GRAPHQL_ADMIN_SECRET: my-super-secret-password
      ENVIRONMENT: ${ENVIRONMENT}
      NODE_ENV: ${NODE_ENV}
      PORT: 5000
  hasura:
    ports:
      - 8080:8080
      - 9691:9691
    build:
      context: ./hasura
      dockerfile: .docker/Dockerfile.stg
    depends_on:
      - db
    environment:
      ACTION_BASE_URL: http://actions:5000
      HASURA_GRAPHQL_ACTIONS_HANDLER_WEBHOOK_BASEURL: http://actions:5000
      HASURA_GRAPHQL_ADMIN_SECRET: my-super-secret-password
      HASURA_GRAPHQL_CONTAINER_HOST_PORT: 8080
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
      HASURA_GRAPHQL_UNAUTHORIZED_ROLE: "public"
      DB_NAME: $DB_NAME
      HASURA_GRAPHQL_DATABASE_URL: "postgres://postgres:postgres@db:5432/$DB_NAME"

volumes:
  db_data:

The actions are an extension of Hasura that require Hasura to be up and running before they can be properly set up. Here is what the docker file looks like:

FROM node
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
CMD ["npm", "run", "graphql", "&&", "npm", "run", "start"]

The yarn graphql file downloads the graphql schema from Hasura using graphql-codegen.

Is it possible to orchestrate docker to wait for the Hasura instance to be ready before building the actions? Or do I need a bash script, and if so what would that look like and be run? What I am looking for is a solution where npm run graphql is continuously rerun until it is able to download the graphql schema from Hasura, then run npm run start.

I am a little out of my depth so any insights or tips are appreciated. I have tried storing the graphql schema locally (so I don't need to wait for Hasura to be ready to get it) however this doesn't work practically as I need Hasura and the actions to be in sync (hence get the schema from Hasura at build time). I have also reach out to the team at graphql-coden and they mention there is no CLI flags or config that allow their code to keep retrying to download the schema until it is ready.


Solution

  • I was able to solve this by using the accepted solution for this question: Keep retrying yarn script until it passes.