postgresql - 无法使用 sequelize 通过端口映射连接到 docker 中的 postgres

标签 postgresql docker docker-compose sequelize.js

我正在 docker 容器中运行 postgres 图像,并尝试使用 docker-compose 从另一个容器连接到它。当我使用 5432 的标准端口时,我能够正常连接,但是当我尝试使用非标准端口和端口映射时,我收到 ECONNREFUSED 错误。

这是我的撰写文件:

networks:
  production-net:
    driver: bridge
services:
  conn-test:
    depends_on:
    - db
    environment:
      DB_DIALECT: postgres
      DB_HOST: db
      DB_NAME: db-name
      DB_PASSWORD: pass
      DB_PORT: '54321'
      DB_USER: user
    image: my-image
    networks:
      production-net: null
  db:
    environment:
      PGDATA: /pgdata
      POSTGRES_DB: db-name
      POSTGRES_PASSWORD: pass
      POSTGRES_USER: user
    image: postgres
    networks:
      production-net: null
    ports:
    - 54321:5432/tcp
version: '3.0'

这是我运行 docker-compose up 时的输出

Creating network "composer_production-net" with driver "bridge"
Creating composer_db_1 ... done
Creating composer_conn-test_1 ... done
Attaching to composer_db_1, composer_conn-test_1
db_1         | The files belonging to this database system will be owned by user "postgres".
db_1         | This user must also own the server process.
db_1         |
db_1         | The database cluster will be initialized with locale "en_US.utf8".
db_1         | The default database encoding has accordingly been set to "UTF8".
db_1         | The default text search configuration will be set to "english".
db_1         |
db_1         | Data page checksums are disabled.
db_1         |
db_1         | fixing permissions on existing directory /pgdata ... ok
db_1         | creating subdirectories ... ok
db_1         | selecting default max_connections ... 100
db_1         | selecting default shared_buffers ... 128MB
db_1         | selecting dynamic shared memory implementation ... posix
db_1         | creating configuration files ... ok
db_1         | running bootstrap script ... ok
db_1         | performing post-bootstrap initialization ... ok
conn-test_1  | Connecting to database
db_1         | syncing data to disk ...
db_1         | WARNING: enabling "trust" authentication for local connections
db_1         | You can change this by editing pg_hba.conf or using the option -A, or
db_1         | --auth-local and --auth-host, the next time you run initdb.
db_1         | ok
db_1         |
db_1         | Success. You can now start the database server using:
db_1         |
db_1         |     pg_ctl -D /pgdata -l logfile start
db_1         |
db_1         | waiting for server to start....2019-02-28 20:52:02.208 UTC [41] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1         | 2019-02-28 20:52:02.534 UTC [42] LOG:  database system was shut down at 2019-02-28 20:51:58 UTC
db_1         | 2019-02-28 20:52:02.618 UTC [41] LOG:  database system is ready to accept connections
db_1         |  done
db_1         | server started
db_1         | CREATE DATABASE
db_1         |
db_1         |
db_1         | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1         |
db_1         | waiting for server to shut down....2019-02-28 20:52:04.355 UTC [41] LOG:  received fast shutdown request
db_1         | 2019-02-28 20:52:04.432 UTC [41] LOG:  aborting any active transactions
db_1         | 2019-02-28 20:52:04.436 UTC [41] LOG:  background worker "logical replication launcher" (PID 48) exited with exit code 1
db_1         | 2019-02-28 20:52:04.436 UTC [43] LOG:  shutting down
db_1         | 2019-02-28 20:52:04.853 UTC [41] LOG:  database system is shut down
db_1         |  done
db_1         | server stopped
db_1         |
db_1         | PostgreSQL init process complete; ready for start up.
db_1         |
db_1         | 2019-02-28 20:52:04.941 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1         | 2019-02-28 20:52:04.941 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1         | 2019-02-28 20:52:05.091 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1         | 2019-02-28 20:52:05.342 UTC [59] LOG:  database system was shut down at 2019-02-28 20:52:04 UTC
db_1         | 2019-02-28 20:52:05.419 UTC [1] LOG:  database system is ready to accept connections
conn-test_1  | Error during start up process
conn-test_1  | connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  |     at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
conn-test_1  |     at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
conn-test_1  |     at emitOne (events.js:116:13)
conn-test_1  |     at Connection.emit (events.js:211:7)
conn-test_1  |     at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
conn-test_1  |     at emitOne (events.js:116:13)
conn-test_1  |     at Socket.emit (events.js:211:7)
conn-test_1  |     at emitErrorNT (internal/streams/destroy.js:66:8)
conn-test_1  |     at _combinedTickCallback (internal/process/next_tick.js:139:11)
conn-test_1  |     at process._tickCallback (internal/process/next_tick.js:181:9)
conn-test_1  |   name: 'SequelizeConnectionRefusedError',
conn-test_1  |   parent:
conn-test_1  |    { Error: connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
conn-test_1  |      errno: 'ECONNREFUSED',
conn-test_1  |      code: 'ECONNREFUSED',
conn-test_1  |      syscall: 'connect',
conn-test_1  |      address: '172.26.0.2',
conn-test_1  |      port: 54321 },
conn-test_1  |   original:
conn-test_1  |    { Error: connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
conn-test_1  |      errno: 'ECONNREFUSED',
conn-test_1  |      code: 'ECONNREFUSED',
conn-test_1  |      syscall: 'connect',
conn-test_1  |      address: '172.26.0.2',
conn-test_1  |      port: 54321 } }
conn-test_1  | Shutting down the application ...

我已经确认 docker 主机正在解析正确的 IP 地址,但它无法连接。

谁能指出我在这里缺少什么?

最佳答案

当您在容器之间直接通信时,您使用目标容器内的服务正在监听的端口号。如果您有 ports: 声明或 docker run -p 选项,则它是第二个端口号;但在此设置中,除非您想从 Docker 空间之外访问服务,否则该设置是严格可选的。

因此,您应该将 DB_PORT: '5432' 设置为指向“普通”PostgreSQL 端口,即使您已将其发布到不同端口号的主机。

关于postgresql - 无法使用 sequelize 通过端口映射连接到 docker 中的 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54934342/

相关文章:

Docker:为什么在 Windows 上使用 Linux 容器?

Docker:无法在 --net=host 模式下链接容器

docker - Docker Compose将一项服务附加到stdin和stdout

macos - 如何在 OS X 上使用 Docker 运行 Kafka?

mysql - 如何在 Docker 中保持数据存储的可移植性?

arrays - 如何在 PostgreSQL 12 中将元素添加到 jsonb 数组中?

postgresql - 使用动态键在 JSON 字段上建立索引

postgresql - 如何在 Grafana 中获取正确的图表

mysql - sql - PostgreSQL 9.6 中的 GROUP_CONCAT

docker - 如何在没有dockerhub的情况下获取可转移的docker compose堆栈