title: dockerized NodeJS application server throws error on unitialized database
body: <p>I have 3 components to my app:</p>
  <pre><code>├── api (backend)
  ├── app (frontend)
  ├── db (database for api)
  </code></pre>
  <p>These are all defined in <code>docker-compose.yml</code> file:</p>
  <pre><code>version: '3.8'
  
  services:
    app:
      container_name: app
      env_file: ./.env
      build:
        context: ./app
      volumes:
        - ./app:/app
      ports:
        - '${APP_PORT}:${APP_PORT}'
      environment:
        - API_PORT=${API_PORT}
        - API_HOST=api
        - PORT=${APP_PORT}
      init: true
      depends_on:
        - api
    api:
      container_name: api
      env_file: ./.env
      build:
        context: ./api
      volumes:
        - ./api:/api
      environment:
        - POSTGRES_USER=${POSTGRES_USER}
        - POSTGRES_DB=${POSTGRES_DB}
        - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        - POSTGRES_PORT=${POSTGRES_PORT}
        - POSTGRES_HOST=db
        - PORT=${API_PORT}
        - APP_HOST=app
        - APP_PORT=${APP_PORT}
      ports:
        - '${API_PORT}:${API_PORT}'
        - '9229:9229'
      depends_on:
        - db
    db:
      container_name: db
      env_file: ./.env
      image: 'postgres:12'
      volumes: 
        - ./postgresql.conf:/etc/postgresql.conf
        - ./pg-data:/var/lib/postgresql/data
      environment:
        - POSTGRES_USER=${POSTGRES_USER}
        - POSTGRES_DB=${POSTGRES_DB}
        - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        - POSTGRES_PORT=${POSTGRES_PORT}
      ports:
        - '${POSTGRES_PORT}:${POSTGRES_PORT}'
  </code></pre>
  <p>I'm having issues with <code>api</code> container when running <code>docker-compose up</code>. This is a NodeJS Express server. This Express server connects to <code>db</code> container which is a postgres database.</p>
  <p>When the whole container (which uses 3 images) is started up for the first time and database is not initialized, it takes up some time for postgres to start up, set up data directories etc. However the server app connects to database right away so it can access it which throws this error:</p>
  <pre><code>api    | node:internal/process/promises:279
  api    |             triggerUncaughtException(err, true /* fromPromise */);
  api    |             ^
  api    |
  api    | Error: connect ECONNREFUSED 172.24.0.2:5432
  api    |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
  api    |   errno: -111,
  api    |   code: 'ECONNREFUSED',
  api    |   syscall: 'connect',
  api    |   address: '172.24.0.2',
  api    |   port: 5432
  api    | }
  
  </code></pre>
  <p>This can be fixed by restarting <code>api</code> service bit later. I would like to know what is the recommended solution. I expected that when postgres is warming up that any requests to the server process will be waiting to be processed but that's obviously not the case.</p>
  <p>Should I use <code>is-port-reachable</code> dependency? I think <a href="https://stackoverflow.com/a/66114637/3056783">this</a> answer to similar question could be a solution but I'm not sure if that is the correct approach. What if the <code>api</code> service wants to shutdown or there are already requests hitting the endpoints which need to access the database?</p>