【问题标题】:Node Postgres Docker节点 Postgres Docker
【发布时间】:2018-03-21 20:27:33
【问题描述】:

我尝试使用我的 node.js 应用程序设置 Docker,该应用程序使用 sequelize 连接到 postgres。

const sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USER,
    process.env.DB_PASS,
    {
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        dialect: 'postgres',
    },
);

在我的.env 文件中,我声明 DB_HOST=postgres(这是在 docker-conpose.yml 中声明的服务的名称)和 DB_PORT=5432 以及所有其他环境变量。

我的Dockerfile 如下所示:

FROM node:8.6.0

# Working directory for application
WORKDIR /usr/src/app
EXPOSE 8080
COPY . /usr/src/app

# In this file I create a user and a DB and give him the privlages 
ADD init.sql /docker-entrypoint-initdb.d/
RUN npm install

而我的docker-compose.yml 如下所示:

version: "2"
services:
  postgres:
    image: "postgres:9.4"
    restart: always
    ports:
      - "5432:5432"
    env_file:
      - .env
  node:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    command: ["npm", "start"]

当我docker-compose up 时,我收到 Sequelize 无法连接到数据库的错误。

未处理的拒绝 SequelizeConnectionRefusedError: 连接 ECONNREFUSED 172.18.0.2:5431

有人可以帮我解决这个错误吗?

所有 docker 日志:

警告:服务节点的映像已构建,因为它尚不存在。要重建此映像,您必须使用 docker-compose builddocker-compose up --build

正在创建 graphqlpostgrestemplate_postgres_1 ...

正在创建 graphqlpostgrestemplate_postgres_1 ... 完成

正在创建 graphqlpostgrestemplate_node_1 ...

正在创建 graphqlpostgrestemplate_node_1 ... 完成

附加到graphqlpostgrestemplate_postgres_1、graphqlpostgrestemplate_node_1

postgres_1 |属于该数据库系统的文件将归用户“postgres”所有。

postgres_1 |该用户还必须拥有服务器进程。

postgres_1 |

postgres_1 |数据库集群将使用语言环境“en_US.utf8”进行初始化。

postgres_1 |默认数据库编码已相应设置为“UTF8”。

postgres_1 |默认文本搜索配置将设置为“英语”。

postgres_1 |

postgres_1 |数据页校验和被禁用。

postgres_1 |

postgres_1 |修复现有目录 /var/lib/postgresql/data 的权限...好的

postgres_1 |创建子目录...好的

postgres_1 |选择默认的 max_connections ... 100

postgres_1 |选择默认的 shared_buffers ... 128MB

postgres_1 |选择动态共享内存实现... posix

postgres_1 |创建配置文件...好的

postgres_1 |在 /var/lib/postgresql/data/base/1 中创建 template1 数据库 ...好的

postgres_1 |初始化 pg_authid ... 好的

postgres_1 |初始化依赖...好的

postgres_1 |创建系统视图...好的

节点_1 | npm info 如果它以 ok 结尾,它就起作用了

节点_1 |使用 npm@5.3.0 的 npm 信息

节点_1 |使用 node@v8.6.0 的 npm 信息

postgres_1 |加载系统对象的描述...好的

节点_1 | npm info 生命周期 graphql-postgres-template@1.0.0~prestart: graphql-postgres-template@1.0.0

节点_1 | npm info 生命周期 graphql-postgres-template@1.0.0~start: graphql-postgres-template@1.0.0

节点_1 |

节点_1 | > graphql-postgres-template@1.0.0 启动/usr/src/app

节点_1 | > nodemon --exec babel-node index.js

节点_1 |

postgres_1 |创建排序规则...好的

postgres_1 |创建转化...好的

postgres_1 |创建字典...好的

postgres_1 |设置内置对象的权限...好的

postgres_1 |创建信息架构...好的

postgres_1 |正在加载 PL/pgSQL 服务器端语言...好的

节点_1 | [nodemon] 1.12.1

节点_1 | 【nodemon】随时重启,输入rs

节点_1 | [nodemon] 观看:.

节点_1 | [nodemon] 开始babel-node index.js

postgres_1 |清理数据库模板1 ...好的

postgres_1 |将模板1复制到模板0 ...好的

postgres_1 |将 template1 复制到 postgres ... 好的

postgres_1 |正在同步数据到磁盘...好的

postgres_1 |

postgres_1 |成功。您现在可以使用以下命令启动数据库服务器:

postgres_1 |

postgres_1 | postgres -D /var/lib/postgresql/data

postgres_1 |或

postgres_1 | pg_ctl -D /var/lib/postgresql/data -l 日志文件开始

postgres_1 |

postgres_1 |

postgres_1 |警告:为本地连接启用“信任”身份验证

postgres_1 |您可以通过编辑 pg_hba.conf 或使用选项 -A 来更改此设置,或者

postgres_1 | --auth-local 和 --auth-host,下次运行 initdb 时。

postgres_1 | ****************************************************** **

postgres_1 |警告:没有为数据库设置密码。

postgres_1 |这将允许任何有权访问的人

postgres_1 | Postgres 端口来访问您的数据库。在

postgres_1 | Docker的默认配置,这个是

postgres_1 |有效地在同一容器上的任何其他容器

postgres_1 |系统。

postgres_1 |

postgres_1 |使用“-e POSTGRES_PASSWORD=password”设置

postgres_1 |它在“docker run”中。

postgres_1 | ****************************************************** **

postgres_1 |等待服务器启动....LOG:无法绑定 IPv6 套接字:无法分配请求的地址

postgres_1 |提示:是否有另一个 postmaster 已经在端口 5432 上运行?如果没有,请等待几秒钟,然后重试。

postgres_1 | LOG:数据库系统于 2017-10-10 12:17:15 UTC 关闭

postgres_1 |日志:Mu​​ltiXact 成员环绕保护现已启用

postgres_1 | LOG:数据库系统已准备好接受连接

postgres_1 | LOG:autovacuum 启动器已启动

postgres_1 |完成

postgres_1 |服务器启动

postgres_1 |改变角色

postgres_1 |

postgres_1 |

postgres_1 | /usr/local/bin/docker-entrypoint.sh:忽略/docker-entrypoint-initdb.d/*

postgres_1 |

postgres_1 |等待服务器关闭....LOG:收到快速关闭请求

postgres_1 | LOG:中止任何活动的事务

postgres_1 | LOG:autovacuum 启动器正在关闭

postgres_1 |日志:正在关闭

postgres_1 | LOG:数据库系统已关闭

节点_1 | Tue, 10 Oct 2017 12:17:16 GMT sequelize deprecated 基于字符串的运算符现已弃用。请使用基于符号的运算符以获得更好的安全性,在 node_modules/sequelize/lib/sequelize.js:236:13 上的 http://docs.sequelizejs.com/manual/tutorial/querying.html#operators 阅读更多内容

节点_1 |警告:在配置目录中找不到配置:/usr/src/app/config

节点_1 |警告:要禁用此警告,请在环境中设置 SUPPRESS_NO_CONFIG_WARNING。

节点_1 | 2017 年 10 月 10 日星期二 12:17:17 GMT body-parser deprecated undefined extended:在 index.js:53:30 处提供扩展选项

节点_1 |未处理的拒绝 SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431

节点_1 |在connection.connect.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24)

节点_1 |在 Connection.connectingErrorHandler (/usr/src/app/node_modules/pg/lib/client.js:123:14)

节点_1 |在 emitOne (events.js:115:13)

节点_1 |在 Connection.emit (events.js:210:7)

节点_1 |在套接字。 (/usr/src/app/node_modules/pg/lib/connection.js:71:10)

节点_1 |在 emitOne (events.js:115:13)

节点_1 |在 Socket.emit (events.js:210:7)

节点_1 |在 emitErrorNT (internal/streams/destroy.js:64:8)

节点_1 |在 _combinedTickCallback (internal/process/next_tick.js:138:11)

节点_1 |在 process._tickDomainCallback (internal/process/next_tick.js:218:9)

节点_1 | [nodemon] 干净退出 - 重启前等待更改

postgres_1 |完成

postgres_1 |服务器停止

postgres_1 |

postgres_1 | PostgreSQL 初始化过程完成;准备启动。

postgres_1 |

postgres_1 | LOG:数据库系统于 2017-10-10 12:17:16 UTC 关闭

postgres_1 |日志:Mu​​ltiXact 成员环绕保护现已启用

postgres_1 | LOG:数据库系统已准备好接受连接

postgres_1 | LOG:autovacuum 启动器已启动

【问题讨论】:

  • 你能发布你的 docker 日志吗?

标签: node.js postgresql docker sequelize.js


【解决方案1】:

到您的Docker-compose 中添加link 服务配置选项node 指向服务postgres 类似这样:

node:
  links:
    - postgres

然后您可以使用名称服务postgres

连接到postgresdb

【讨论】:

  • 感谢您的快速回复。更新 docker-compose 文件后,我收到一个新的 Sequelize 错误。 > 未处理的拒绝 SequelizeConnectionRefusedError: 连接 ECONNREFUSED 172.18.0.2:5431 node_1 |在 connection.connect.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24) node_1 |在 Connection.connectingErrorHandler (/usr/src/app/node_modules/pg/lib/client.js:123:14) node_1 |在 emitOne (events.js:115:13) node_1 |在 Connection.emit (events.js:210:7) node_1 |在 Socket.
  • 确保process.env.DB_HOST 已将服务postgres 设置为值。
猜你喜欢
  • 2021-01-07
  • 2021-06-08
  • 2020-09-20
  • 2020-03-08
  • 2022-01-01
  • 2020-09-12
  • 2016-12-09
  • 2021-09-10
  • 1970-01-01
相关资源
最近更新 更多