首页 文章

节点Postgres Docker

提问于
浏览
1

我尝试使用我的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无法连接到DB的错误 .

未处理拒绝SequelizeConnectionRefusedError:connect ECONNREFUSED 172.18.0.2:5431

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

所有的码头日志:

警告:已构建服务节点的映像,因为它尚不存在 . 要重建此映像,您必须使用docker-compose build或docker-compose up --build . 创建graphqlpostgrestemplate_postgres_1 ...创建graphqlpostgrestemplate_postgres_1 ... done创建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 |默认文本搜索配置将设置为“english” . postgres_1 | postgres_1 |数据页校验和已禁用 . postgres_1 | postgres_1 |修复现有目录/ var / lib / postgresql / data的权限... ok postgres_1 |创建子目录... ok postgres_1 |选择默认的max_connections ... 100 postgres_1 |选择默认的shared_buffers ... 128MB postgres_1 |选择动态共享内存实现... posix postgres_1 |创建配置文件... ok postgres_1 |在/ var / lib / postgresql / data / base / 1中创建template1数据库... ok postgres_1 |初始化pg_authid ... ok postgres_1 |初始化依赖... ok postgres_1 |创建系统视图... ok node_1 | npm info如果它以ok node_1 |结束它是否有效npm info使用npm@5.3.0 node_1 | npm info使用node@v8.6.0 postgres_1 |加载系统对象的描述...... ok node_1 | npm info lifecycle graphql-postgres-template@1.0.0~prestart:graphql-postgres-template@1.0.0 node_1 | npm info lifecycle graphql-postgres-template@1.0.0 ~start:graphql-postgres-template@1.0.0 node_1 | node_1 | > graphql-postgres-template@1.0.0 start / usr / src / app node_1 | > nodemon --exec babel-node index.js node_1 | postgres_1 |创建校对...好postgres_1 |创建转换...确定postgres_1 |创建字典...好postgres_1 |设置内置对象的权限... ok postgres_1 |创建信息架构...好postgres_1 |加载PL / pgSQL服务器端语言... ok node_1 | [nodemon] 1.12.1 node_1 | [nodemon]随时重启,输入rs node_1 | [nodemon]观看: . node_1 | [nodemon]启动babel-node index.js postgres_1 |吸尘数据库template1 ... ok postgres_1 |将template1复制到template0 ... ok postgres_1 |将template1复制到postgres ... ok postgres_1 |将数据同步到磁盘...... ok 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 logfile start 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 |它在“码头运行”中 . postgres_1 | ************************************************** ** postgres_1 |等待服务器启动....日志:无法绑定IPv6套接字:无法分配请求的地址postgres_1 |提示:另一位邮局管理员是否已在5432端口上运行?如果没有,请等待几秒钟重试 . postgres_1 |日志:数据库系统在2017-10-10 12:17:15 UTC postgres_1 |关闭日志:现在启用了postgres_1 |的MultiXact成员环绕保护日志:数据库系统已准备好接受postgres_1 |的连接日志:autovacuum启动器启动postgres_1 |完成postgres_1 |服务器启动了postgres_1 | ALTER ROLE postgres_1 | postgres_1 | postgres_1 | /usr/local/bin/docker-entrypoint.sh:忽略/docker-entrypoint-initdb.d/* postgres_1 | postgres_1 |等待服务器关闭....日志:收到快速关机请求postgres_1 |日志:中止任何活动的交易postgres_1 |日志:autovacuum Launcher 关闭postgres_1 |日志:关闭postgres_1 |日志:数据库系统关闭node_1 | 2017年10月10日星期二12:17:16 GMT sequelize不推荐使用基于字符串的运算符现已弃用 . 请使用基于符号的运算符以获得更好的安全性,请在http://docs.sequelizejs.com/manual/tutorial/querying.html#operators上阅读更多内容:node_modules / sequelize / lib / sequelize.js:236:13 node_1 |警告:在配置目录中找不到任何配置:/ usr / src / app / config node_1 |警告:要禁用此警告,请在环境中设置SUPPRESS_NO_CONFIG_WARNING . node_1 | 2017年10月10日星期二12:17:17 GMT身体解析器弃用未定义扩展:在index.js提供扩展选项:53:30 node_1 |未处理的拒绝SequelizeConnectionRefusedError:connect ECONNREFUSED 172.18.0.2:5431 node_1 | at 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 . (/usr/src/app/node_modules/pg/lib/connection.js:71:10)node_1 |在emitOne(events.js:115:13)node_1 |在Socket.emit(events.js:210:7)node_1 |在emitErrorNT(internal / streams / destroy.js:64:8)node_1 |在_combinedTickCallback(internal / process / next_tick.js:138:11)node_1 | at process._tickDomainCallback(internal / process / next_tick.js:218:9)node_1 | [nodemon] clean exit - 在重启postgres_1之前等待更改完成postgres_1 |服务器已停止postgres_1 | postgres_1 | PostgreSQL init进程完成;准备好开始了 . postgres_1 | postgres_1 |日志:数据库系统在2017-10-10 12:17:16 UTC postgres_1 |关闭日志:现在启用了postgres_1 |的MultiXact成员环绕保护日志:数据库系统已准备好接受postgres_1 |的连接日志:autovacuum Launcher 开始了

1 回答

  • 1

    在您的服务中添加 Docker-compose 添加 link 配置选项 node 指向服务 postgres ,如下所示:

    node:
      links:
        - postgres
    

    然后你可以用名称服务 postgres 连接到 postgresdb

相关问题