# docker compose make sure postgres is ready before starting other service

Sometimes we want to start a service in docker compose after some dependency is ready. In this case, we want to wait for PostgreSQL.

The key here is `healthcheck` on the PostgreSQL

```yaml
healthcheck:
  test: [ "CMD-SHELL", "pg_isready -U postgres" ]
  interval: 10s
  timeout: 5s
  retries: 5
```

and `depends_on` on the service

```yaml
depends_on:
  postgres:
    condition: service_healthy
```

Full docker-compose.yaml

```yaml
version: '3.8'
services:
  postgres:
    image: postgres:11.6-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 12345
      POSTGRES_DB: todo
    ports:
      - 5432:5432
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 10s
      timeout: 5s
      retries: 5

  backend:
    build: backend
    environment:
      DB_HOST: postgres
    ports:
      - 5000:5000
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  postgres_volume:
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://til.yulrizka.com/docker/docker-compose-make-sure-postgres-is-ready-before-starting-other-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
