0

When I first time build containers database doesn't have enough time to initialize itself while web service and nginx is already up and thus I can't reach the server from a first run, but after second containers run everything works properly. I have tried this command: ["./wait-for-it.sh", "db:5432", "--", "python", "manage.py runserver 0.0.0.0:8000"] to wait until database got initialized, but it didn't help me. Help me please to make my services wait until database get initialized. I've tried solutions from this post, but nothing was helpful. Help me please to make my services wait until database get initialized. Thanks in advance!

Here is my docker-compose file

version: "3.9"

services:
  db:
    image: postgres:13.3-alpine
    container_name: db
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:5432" ]
      interval: 30s
      timeout: 10s
      retries: 5
  web:
    build: .
    container_name: web
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    restart: on-failure
    depends_on:
      - db
  nginx:
    build: ./nginx
    container_name: nginx
    ports:
      - "80:80"
    restart: on-failure
    depends_on:
      - web
      - db
Artem
  • 15
  • 7
  • if this is in a swarm then it might happen, try using a retry mechanism. – Eitank Aug 08 '21 at 12:33
  • It's not in a swarm @Eitank – Artem Aug 08 '21 at 12:37
  • The question you've linked to is the canonical question for this topic. Can you edit this question to show what you've already tried, and what incorrect error or behavior you're getting? (Does the `restart: on-failure` for the `web` container make everything start up correctly? Correctly breaking your proposed `command:` into words?) – David Maze Aug 08 '21 at 13:04

2 Answers2

1

depends_on only waits until the service has started, not until it is healthy. You should try to additionally define the condition service_healthy to wait until a dependency is healthy:

depends_on:
  db:
     condition: service_healthy

Here's a complete docker-compose file for reference:

version: "3.9"

services:
  db:
    image: postgres:13.3-alpine
    container_name: db
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 1s
      timeout: 5s
      retries: 5
  web:
    image: nginx:latest
    container_name: web
    restart: on-failure
    depends_on:
      db:
        condition: service_healthy
helios35
  • 1,577
  • 1
  • 14
  • 27
  • Version 3 of docker-compose no longer supports the `condition` form of `depends_on`. More info [here](https://stackoverflow.com/questions/47710767/what-is-the-alternative-to-condition-form-of-depends-on-in-docker-compose-versio) – swimmer Nov 17 '21 at 21:46
  • @Ignacio it works as intended, I've tested it with docker-compose 2.2.2. The `condition: service_healthy` part is documented [here](https://github.com/compose-spec/compose-spec/blob/e8db8022c0b2e3d5eb007d629ff684cbe49a17a4/spec.md) – helios35 Dec 11 '21 at 19:40
0

Problem solved by adding the short line of script to command.

  web:
    build: .
    container_name: web
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; python manage.py runserver 0.0.0.0:8000'
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    restart: on-failure
    depends_on:
      - db
Artem
  • 15
  • 7