简体   繁体   English

无法在 docker-compose 中使用 Postgres DB - ECONNREFUSED

[英]Unable to use Postgres DB in docker-compose - ECONNREFUSED

I have a Node.js app and have set up docker-compose.我有一个 Node.js 应用程序并设置了 docker-compose。 I'm trying to use Postgres DB in the container.我正在尝试在容器中使用 Postgres DB。

Here is docker-compose这是 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
    }

If you connect to the database from another docker container, use the service name in the connection string( db in your case).如果您从另一个 docker 容器连接到数据库,请在连接字符串中使用服务名称(在您的情况下为db )。 From the error message it seems that there is something wrong with your configuration.从错误消息来看,您的配置似乎有问题。 It tries to connect to localhost ( 127.0.0.1 )它尝试连接到 localhost ( 127.0.0.1 )

For those, you use more recent structure project with a config file like below:对于那些,您可以使用带有如下配置文件的更新的结构项目:

require('dotenv').config();

module.exports = {
    development: {
        username: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_NAME,
        host: process.env.APP_URL_DOCKER,
        dialect: 'postgres'
    },
    test: {
        username: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_NAME,
        host: process.env.APP_URL_DOCKER,
        dialect: 'postgres'
    },
    production: {
        username: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_NAME,
        host: process.env.APP_URL_DOCKER,
        dialect: 'postgres'
    }
};

You can specify the service name of your postgres database in your .env file.您可以在.env文件中指定postgres database的服务名称。 And it should be good for you.它应该对你有好处。

// .env file
APP_URL_LOCAL=127.0.0.1
APP_URL_DOCKER=db # database service name
APP_PORT=8000
APP_KEY=6dd63668dab9a309c7c59a2982126a43b064816925a40b9680d16e2665f41b676a87dae4d506712e6332479c82827993059ddefb607b65caa3534b66b20253ed

DB_USER=postgres
DB_PASSWORD=db_password
DB_NAME=db_name
DB_PORT_CONTAINER=5432
DB_PORT_LOCAL=5433

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM