【问题标题】:NodeJS API not communicating with PostgreSQL in Docker-composeNodeJS API 未在 Docker-compose 中与 PostgreSQL 通信
【发布时间】:2020-09-10 04:54:58
【问题描述】:

我正在使用 docker、postgreSQL、NodeJS 和 redis 创建一个简单的 webapp。节点 API 正在与 redis 服务器连接,但未与 postgreeSQL 连接。 Redis 和 postgreSQL 都是 docker-compose 中使用的服务,并且设置了所有环境变量。 下面是docker-compose.yml

version: '3'
services:
    db:
        image: 'postgres:latest'
        environment: 
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres_password
            - POSTGRES_DB=postgres
    redis:
        image: 'redis:latest'
    nginx:
        restart: always
        build:
            dockerfile: Dockerfile.dev
            context: ./nginx
        ports:
            - '4000:80'
    api:
        build:
            dockerfile: Dockerfile.dev
            context: ./server
        volumes:
            - /app/node_modules
            - ./server:/app
        environment:
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - PGUSER=postgres
            - PGHOST=db
            - PGDATABASE=postgres
            - PGPASSWORD=postgres_password
            - PGPORT=5432
    client:
        build:
            dockerfile: Dockerfile.dev
            context: ./client
        volumes:
            - /app/node_modules
            - ./client:/app
    worker:
        build:
            dockerfile: Dockerfile.dev
            context: ./worker
        environment: 
            - REDIS_HOST=redis
            - REDIS_PORT=6379
        volumes: 
            - /app/node_modules
            - ./worker:/app

节点 API index.js 文件:

const keys = require('./keys');

// Express App Setup
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
app.use(cors());
app.use(bodyParser.json());

// Postgres Client Setup
const { Pool } = require('pg');
const pgClient = new Pool({
    user: keys.pgUser,
    host: keys.pgHost,
    database: keys.pgDatabase,
    password: keys.pgPassword,
    port: keys.pgPort
});

pgClient.on('error', () => console.log('Lost PG connection'));

pgClient
    .query('CREATE TABLE IF NOT EXISTS values (number INT)', (err, res) => {
        if (!err) {
            console.log("TABLE CREATES SUCCESSFULLY", res);
        }
        else {
            console.log(err)
        }
    })

// Redis Client Setup
const redis = require('redis');
const redisClient = redis.createClient({
    host: keys.redisHost,
    port: keys.redisPort,
    retry_strategy: () => 1000
});
const redisPublisher = redisClient.duplicate();

// Express route handlers

console.log("INSIDE EXPRESS");

pgClient.query('SELECT NOW() as now', (err, res) => {
    if (err) {
        console.log("ERROR STACK", err.stack)
    } else {
        console.log("RESULT STACK", res.rows[0])
    }
})

app.get('/', (req, res) => {
    res.send('Hi');
});

app.get('/values/all', async (req, res) => {
      const values = await pgClient.query('SELECT * FROM values');
    // const values = [1, 2, 3, 4];

    res.status(200).send(values);
});

app.get('/values/current', async (req, res) => {
    redisClient.hgetall('values', (err, values) => {
        res.send(values);
    });
});

app.post('/values', async (req, res) => {
    const index = req.body.index;

    if (parseInt(index) > 40) {
        return res.status(422).send('Index too high');
    }

    redisClient.hset('values', index, 'Nothing yet!');
    redisPublisher.publish('insert', index);
    pgClient.query('INSERT INTO values(number) VALUES($1)', [index]);

    res.send({ working: true });
});

app.listen(5000, err => {
    console.log('Listening');
});

还有,用于 API 的 DockerFile.dev:

FROM node:alpine

WORKDIR '/app'

COPY ./package.json ./
RUN npm install

COPY . .

CMD ["npm", "run", "dev"]

我正在使用 nginx 将 docker-compose 连接到外部世界,它工作得非常好。 API 正在响应 Redis 服务器,因此 API 连接没有故障。 postgreSQL 的结果(日志)如下所示:

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.


The database cluster will be initialized with locale "en_US.utf8".

The default database encoding has accordingly been set to "UTF8".

The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok

creating subdirectories ... ok

selecting dynamic shared memory implementation ... posix

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting default time zone ... Etc/UTC

creating configuration files ... ok

running bootstrap script ... ok

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections

You can change this by editing pg_hba.conf or using the option -A, or

--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....2020-05-23 09:20:15.804 UTC [46] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit

2020-05-23 09:20:15.818 UTC [46] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2020-05-23 09:20:16.031 UTC [47] LOG: database system was shut down at 2020-05-23 09:20:12 UTC

2020-05-23 09:20:16.141 UTC [46] LOG: database system is ready to accept connections

done

server started

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

2020-05-23 09:20:16.205 UTC [46] LOG: received fast shutdown request

waiting for server to shut down....2020-05-23 09:20:16.222 UTC [46] LOG: aborting any active transactions

2020-05-23 09:20:16.229 UTC [46] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1

2020-05-23 09:20:16.243 UTC [48] LOG: shutting down

2020-05-23 09:20:16.397 UTC [46] LOG: database system is shut down

server stopped

PostgreSQL init process complete; ready for start up.

2020-05-23 09:20:16.706 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit

2020-05-23 09:20:16.719 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432

2020-05-23 09:20:16.724 UTC [1] LOG: listening on IPv6 address "::", port 5432

2020-05-23 09:20:16.752 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2020-05-23 09:20:16.853 UTC [55] LOG: database system was shut down at 2020-05-23 09:20:16 UTC

2020-05-23 09:20:16.980 UTC [1] LOG: database system is ready to accept connections

2020-05-23 09:38:08.005 UTC [1] LOG: received smart shutdown request

2020-05-23 09:38:08.708 UTC [1] LOG: background worker "logical replication launcher" (PID 61) exited with exit code 1

2020-05-23 09:38:08.796 UTC [56] LOG: shutting down

2020-05-23 09:38:10.440 UTC [1] LOG: database system is shut down

PostgreSQL Database directory appears to contain a database; Skipping initialization

2020-05-23 09:55:51.503 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit

2020-05-23 09:55:51.513 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432

2020-05-23 09:55:51.513 UTC [1] LOG: listening on IPv6 address "::", port 5432

2020-05-23 09:55:51.558 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2020-05-23 09:55:52.114 UTC [25] LOG: database system was shut down at 2020-05-23 09:38:09 UTC

2020-05-23 09:55:52.190 UTC [1] LOG: database system is ready to accept connections

可以看出,数据库也运行良好,我可以使用 docker cli 与它进行交互。

但我不知道为什么 pgClient 在 Node API 中不起作用。

我已经提供了有关问题的所有详细信息,如果需要其他任何信息,请告诉我。

希望在这里得到一些帮助。

谢谢

【问题讨论】:

  • 可能您的 nodejs 应用程序在完全初始化之前尝试连接到 db。见controlling the startup order
  • 检查了但没用,我用 mongoDB 也试过了,效果很好。

标签: node.js postgresql docker docker-compose


【解决方案1】:

我有同样的错误。

也许,这个日志显示了原因。

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

docker 中的 Postgres 会忽略 entrypoint.sh,因此不会发生初始化过程。

根据this issue,您可以通过删除卷来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2017-01-27
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多