Home > Software engineering >  postgresql not reachable in docker-compose
postgresql not reachable in docker-compose

Time:02-23

I use docker-compose to set up a web service connected to a postgresql database. When I run docker-compose up -d, both mydb and web services are created, but the database is not reachable from the web service. I cannot understand why.

services:
  web:
    image: etherpad/etherpad:1.8.10
    depends_on:
      mydb:
        condition: service_healthy
    ports:
      - "6080:9001"
    environment:
      - DB_TYPE=postgres
      - DB_PORT=5432
      - DB_HOST=mydb
      - DB_NAME=postgres
      - DB_USER=etherpad
      - DB_PASS=mydbpass
  mydb:
    image: postgres:13
    expose:
      - 5432
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_PASSWORD=mydbpass
      - POSTGRES_USER=etherpad
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5

CodePudding user response:

I fixed it with : github.com/moby/moby/issues/36151#issuecomment-968356070

CodePudding user response:

For 2 containers (=instance of service) to communicate you need a network between them.

Indeed depends_on is not enough, it just adds constraints on start-up/build.

services:
  web:
    image: etherpad/etherpad:1.8.10
    depends_on:
      mydb:
        condition: service_healthy
    ports:
      - "6080:9001"
    environment:
      - DB_TYPE=postgres
      - DB_PORT=5432
      - DB_HOST=mydb
      - DB_NAME=postgres
      - DB_USER=etherpad
      - DB_PASS=mydbpass
    networks:
      - backend
  mydb:
    image: postgres:13
    expose:
      - 5432
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_PASSWORD=mydbpass
      - POSTGRES_USER=etherpad
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - backend
networks:
  backend:
  • Related