Docker容器中的服务器无法连接到另一个Docker容器中的Postgres数据库 [英] Server in docker container unable to connect to postgres database in another docker container

查看:469
本文介绍了Docker容器中的服务器无法连接到另一个Docker容器中的Postgres数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点服务器正在尝试使用 knex 连接到Postgres数据库.两者都在具有相同自定义网络的docker容器中.我不断收到ECONNREFUSED错误.我在数据库容器中打了个比方,发现我的数据库(test_db)是由psql创建的,但没有权限.授予root权限后,我仍然遇到相同的问题.我试过用docker-compose down -v删除卷,但还是没有运气.我也尝试过删除knex,仅使用 node-postgres ,但是存在相同的错误.我也无法使用pgadmin从主机连接到Db.将不胜感激!

I have a node server that's trying to connect to a postgres database using knex. Both are in docker containers with the same custom network. I keep getting ECONNREFUSED errors. I've poked around in the database container and I see that my DB (test_db) has been created by psql but it has no permissions. After giving root permissions, I'm still getting the same issues. I've tried removing the volumes with docker-compose down -v but still no luck. I've also tried removing knex and just using node-postgres but same errors. I'm also unable to connect to the Db from the host using pgadmin. Would appreciate any help!

这是我的docker-compose.yml

Here's my docker-compose.yml

version: "3"

services:
  server:
    build:
      context: .
      dockerfile: development.Dockerfile
    ports:
      - "8081:8081"
    volumes:
      - .:/src
      - /src/node_modules
    networks:
      - dev-network
    environment:
      DB_HOSTNAME: pg-development
      DB_USER: root
      DB_PASSWORD: helloworld
      DB_PORT: 3306
      DB_NAME: test_dev
    depends_on:
      - pg-development
  pg-development:
    image: postgres
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: helloworld
      POSTGRES_DB: test_dev
    ports:
      - "3308:3306"
    volumes:
      - dbdata:/data/db
    networks:
      - dev-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
      interval: 10s
      timeout: 2s
      retries: 10
networks:
  dev-network:
    driver: bridge

volumes:
  dbdata:

这是我的数据库连接

import knex from "knex";

const connection = {
  host: process.env.DB_HOSTNAME,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  port: Number(process.env.DB_PORT),
  database: process.env.DB_USER,
};

const db = knex({
  client: "pg",
  connection,
  debug: true,
  pool: {
    min: 0,
    max: 50,
    afterCreate: function (conn, done) {
      conn.query('SET timezone="UTC";', function (err) {
        if (err) {
          done(err, conn);
        }
      });
    },
  },
});

推荐答案

之所以不起作用,是因为数据库实际上是在默认的postgres端口5432上启动的.在docker-compose.yml中可以找到需要添加命令以更改默认端口.

The reason this wasn't working is because the DB was actually being started on the default postgres port which is 5432. Turns out in docker-compose.yml you need to add a command to change the default port.

pg-development:
    image: postgres
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: helloworld
      POSTGRES_DB: test_dev
    ports:
      - "3308:3306"
    volumes:
      - dbdata:/data/db
    networks:
      - dev-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
      interval: 10s
      timeout: 2s
      retries: 10
    command: -p 3306 // this fixes the issue

这篇关于Docker容器中的服务器无法连接到另一个Docker容器中的Postgres数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆