无法使用sequelize通过端口映射连接到Docker中的Postgres

时间:2019-02-28 21:13:00

标签: 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地址,但无法连接。

有人可以指出我在这里想念的东西吗?

2 个答案:

答案 0 :(得分:1)

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

因此,即使您已将其发布到主机上的其他端口号上,也应将DB_PORT: '5432'设置为指向“正常” PostgreSQL端口。

答案 1 :(得分:1)

$label2

这是docker网络内部的端口$label3与计算机网络上的端口ports: - 54321:5432/tcp 的映射,因此当您尝试从另一个容器访问它时,它将失败。

要将容器的端口暴露给同一Docker网络中的其他容器,您需要在docker compose中使用5432或在Dockerfile中使用54321,但是必须将数据库配置为接受其上的连接