【问题标题】:Can't connect to postgres in docker with port mapping using sequelize无法使用 sequelize 通过端口映射连接到 docker 中的 postgres
【发布时间】:2019-02-28 21:13:00
【问题描述】:

我在 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 地址,但它无法连接。

谁能指出我在这里遗漏了什么?

【问题讨论】:

    标签: postgresql docker docker-compose sequelize.js


    【解决方案1】:

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

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

    【讨论】:

    • 我明白了。所以我根本不应该使用ports:,因为我希望它完全存在于 Docker 空间中。
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多