0

I am running into issue an issue connecting to database (postgres, db-server service as mentioned below) running in a container, and I am trying to access database from the other service ( crawler). Looks like it got the ip address right (I am using hostname as db-server) but, its not able to connect to the db. I am stuck on this for a very long time. Please help!

version: "3.7"

services:

    # App Service
  go-server:
    # container_name: go_server
    build: backend
    ports:
      - 8000:8000
    depends_on:
      - db-server
    networks:
      - api-server

  #Database
  db-server:
    # container_name: db_server
    image: postgres
    restart: always
    secrets:
      - db-password
    volumes:
      - ${HOME}/data:/var/lib/postgresql/data
      - ./database/scripts/schema.sql:/docker-entrypoint-initdb.d/schema.sql

    environment:
      - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
    expose:
    - 5432
    networks:
      - api-server
  
  crawler:
    build: crawler
    networks:
      - api-server
  
networks:
  api-server: {}
volumes:
  pg-data: {}
secrets:
  db-password:
    file: database/password.txt
Delta
  • 141
  • 1
  • 8
  • You can hit problems like this if the application starts up before the database is ready; Go applications tend to start very quickly but a database container can take 30-60 seconds to initialize. If you run `docker-compose up` without a `-d` option, do you see database initialization messages after the application gives up? Also see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) for more discussion of this specific problem. – David Maze Jan 18 '22 at 11:34

0 Answers0