【问题标题】:NodeJS is not able to access postgres databaseNodeJS 无法访问 postgres 数据库
【发布时间】:2021-10-27 07:03:00
【问题描述】:

我的app.js文件如下

var express = require('express');
var app = express();
var uuid = require('node-uuid');

var pg = require('pg');
var conString = process.env.DB; // "postgres://username:password@localhost/database";

// Routes
app.get('/api/status', function(req, res) {
  pg.connect(conString, function(err, client, done) {
    if(err) {
      return res.status(500).send('error fetching client from pool');
    }
    client.query('SELECT now() as time', [], function(err, result) {
      //call `done()` to release the client back to the pool
      done();

      if(err) {
        return res.status(500).send('error running query');
      }

      return res.json({
        request_uuid: uuid.v4(),
        time: result.rows[0].time
      });
    });
  });
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.json({
    message: err.message,
    error: {}
  });
});


module.exports = app;

我的Dockerfile如下

FROM node:14

WORKDIR /app

COPY package*.json ./

RUN  npm install

COPY . .

我的docker-compose文件如下:

version: "3.2"
services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: test123
      POSTGRES_DB: testing
    volumes:
      - ./pgdata:/var/lib/postgresql/data
  web:
    build: .
    command: npm start app.js
    depends_on:
      - db
    environment:
      DB: 'postgres://postgres:test123@localhost:5432/testing'
    ports:
      - "3000:3000"

当我检入容器数据库时,我可以看到用户、数据库,但我的 app.js 文件无法从数据库中获取值。 我正在此端点 /api/status 中路由数据 当我访问 http://ip-address:3000 == 时,我收到了我在代码中提到的 404 错误。 如果没有 docker,我可以轻松地在给定端点访问我的输出。

任何帮助都会有所帮助。谢谢

【问题讨论】:

  • 你在http://ip-address:3000容器的IP上调用哪个IP url?尝试使用http://localhost:3000,但如果处于活动状态,请停止本地。
  • 本地机器的 IP 地址,我的容器在不同的 IP 上运行,也许我应该试试这个postgres://postgres:test123@host.docker.internal:5432/testing
  • 我见过的快递应用都有app.listen的电话。你在哪里告诉你的应用监听 3000 端口?
  • 我已经添加到/bin/www
  • 这对 404 错误没有帮助,但是您的前端将无法连接到您的数据库。将数据库连接从 DB: 'postgres://postgres:test123@localhost:5432/testing' 更改为 DB: 'postgres://postgres:test123@db:5432/testing' 当您运行 docker 容器时,localhost 指的是容器本身,而不是您的主机。您需要连接到另一个容器。还要在db服务中添加一个ports部分来暴露5432端口

标签: node.js postgresql docker docker-compose dockerfile


【解决方案1】:

当您要求 docker-compose 启动您的容器时,它会创建一个虚拟网络,其中每个容器都有自己的 IP 地址。当您在 Web 容器中请求连接到 localhost 时,您是在尝试连接到 Web 容器本身而不是数据库容器。

您可以通过其服务名称db 访问数据库容器。所以改变你的 docker-compose 文件从

environment:
  DB: 'postgres://postgres:test123@localhost:5432/testing'

到

environment:
  DB: 'postgres://postgres:test123@db:5432/testing'

【讨论】:

  • 没有成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2022-08-08
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2020-02-12
相关资源
最近更新 更多