Reputation: 1
I am creating a simple webapp with docker, postgreSQL, NodeJS and redis. The node API is connecting with redis server but not connecting with postgreeSQL. Redis and postgreSQL both are used services in docker-compose and all the environment variables are set.
Below is the 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
Node API index.js
file:
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');
});
And, DockerFile.dev for API:
FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
I am using nginx to connect docker-compose to outside world and it is working perfectly fine. The API is responding to Redis server so there is no fault in the API connectiion. The result (logs) of the postgreSQL is given below:
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
As can be seen, the database is also working fine and I am able to interact with it using docker cli.
But I don't know why pgClient
is not working in Node API.
I have provided all the details regarding the problem, if anything else is required please inform me.
Hoping to find some help here.
Thank You
Upvotes: 0
Views: 320
Reputation: 891
I had same error.
Maybe, this log show the reason.
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
Postgres in docker would ignore entrypoint.sh, so initailizing process didn't occur.
According to this issue, you can resolve it by removing volumes.
Upvotes: 1