Warm tip: This article is reproduced from serverfault.com, please click

Unable to use Postgres DB in docker-compose

发布于 2018-06-12 10:14:20

I have a Node.js app and have set up docker-compose. I'm trying to use Postgres DB in the container.

Here is docker-compose

version: "3"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    links:
      - redis
      - db
  angular: # image of nginx
    image: "qp-angular-nginx"
    ports:
      - "4200:80" # specify port forewarding

  redis:
    image: "redis:3"
    ports:
     - "6379:6379"     
  db:
    image: "postgres:9.6"
    ports:
     - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    volumes:
         - pgdata:/var/lib/postgresql/data         
  dbadmin:
    image: "myimage/phppgadmin"
    ports:
         - "8085:80"
    environment:
        POSTGRES_HOST: db
        POSTGRES_PORT: 5432
volumes: 
     pgdata:

I have the containers running successfully and even database connected, but I get this error when making a DB request

 "stack": {
        "code": "ECONNREFUSED",
        "errno": "ECONNREFUSED",
        "syscall": "connect",
        "address": "127.0.0.1",
        "port": 5432
    }
Questioner
Alamgir Qazi
Viewed
0
b0gusb 2018-06-12 18:50:34

If you connect to the database from another docker container, use the service name in the connection string(db in your case). From the error message it seems that there is something wrong with your configuration. It tries to connect to localhost (127.0.0.1)