skip to Main Content

I have a fairly complex application where I have next js on the client and on the backend I have graphql and I have nginx as a reverse proxy.

I am using next JS incremental static site regeneration functionality on the index page so that’s why I want my server up and running before my client container start building because when I run npm run build it is going to fetch some data from the graphql server here is my docker compose file

version: "3"
services:
  mynginx:
    container_name: mynginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 80:80

  graphql:
    container_name: graphql_server
    depends_on:
      - mynginx
    build:
      context: ./server
      dockerfile: Dockerfile

  mynextjs:
    container_name: nextjs_server
    depends_on:
      - graphql
    build:
      context: ./client
      dockerfile: Dockerfile

3

Answers


  1. depends_on with healthcheck, to start container when another already works

    https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck

    something like this

    services:
      mynginx:
        container_name: mynginx
        build:
          context: ./nginx
          dockerfile: Dockerfile
        ports:
          - 80:80
        healthcheck:
          test: ["CMD-SHELL", "wget -O /dev/null http://localhost || exit 1"]
          timeout: 10s
    
      graphql:
        ...
        depends_on:
          depends_on:
            mynginx:
              condition: service_healthy
    
    Login or Signup to reply.
  2. In simple terms, what you need is to wait until graphql service is properly finished starting before you run mynextjs service.

    I’m afraid that depends_on (or links) might not work out of the box in this case. The reason is, although Compose does start graphql service before it starts mynextjs service, it doesn’t wait until graphql service is in READY state to start the dependant services.

    Basically, what you need is to find a way to tell Compose to wait until graphql service is in READY state.

    The solution is described in Control startup and shutdown order in Compose documentation

    I hope this helps you. Cheers 🍻 !!!

    Login or Signup to reply.
  3. If I understand your problem correctly, you need to delay beginning the build of the image for the frontend container, until the backend containers are running and ready.

    The easiest way that comes to mind would be to use profiles to allow starting these independently.

    Eg:

    version: "3"
    services:
      mynginx:
        container_name: mynginx
        build:
          context: ./nginx
          dockerfile: Dockerfile
        ports:
          - 80:80
        profiles: ["backend"]
    
      graphql:
        container_name: graphql_server
        depends_on:
          - mynginx
        build:
          context: ./server
          dockerfile: Dockerfile
        profiles: ["backend"]
    
      mynextjs:
        container_name: nextjs_server
        depends_on:
          - graphql
        build:
          context: ./client
          dockerfile: Dockerfile
        profiles: ["frontend"]
    

    Then chain the starting something like:

    docker-compose --profile backend -d up && docker-compose --profile frontend -d up
    

    Another option might be splitting into two separate compose files, with a shared docker network between them.

    ref: https://docs.docker.com/compose/profiles/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search