2

I have the following docker-compose.yml file which specifies that the backend service should wait until the postgres service is healthy before starting the backend service. Apparently, postgres service is already healthy even if it is still running its startup script.

This is my docker-compose.yml file.

version: "3.7"
services:
  backend:
    build: .
    ports:
      - "8080:8080"
    env_file:
      - .env
    depends_on:
      postgres:
        condition: service_healthy
  postgres:
    image: postgres:13
    ports:
      - "${DB_PORT}:${DB_PORT}"
    env_file:
      - .env
    volumes:
      - ./initdb.d:/docker-entrypoint-initdb.d
      - data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 10s
      retries: 5
volumes:
  data:

I am mounting a startup script that runs for quite a while in the ./initdb.d file. The startup script will populate the DB with 1M rows. It seems like when the startup scripts are running, the backend service can't connect to postgres. Right now, my best solution is simply to add restart: on-failure:5 to wait for the startup scripts to finish. Is there a more robust way to achieve this though?

Richard
  • 7,037
  • 2
  • 23
  • 76
  • The canonical question on this topic is [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y); any of the approaches that depend on a TCP connection from another container won't have this problem. – David Maze Feb 02 '23 at 12:27

0 Answers0