Я пытаюсь Dockerize Node API и получаю сообщение об ошибке только в приложении Dockerized. За пределами Docker, node server.js
работает без проблем. Используя docker-compose up
, я получаю следующую ошибку через 10 минут с вызовами API или без них:
api | [nodemon] starting `node server.js`
api | listening on port 3000
api | API is ready
api | Error: Connection terminated unexpectedly
api | at Connection.<anonymous> (/api/node_modules/pg/lib/client.js:255:9)
api | at Object.onceWrapper (events.js:427:28)
api | at Connection.emit (events.js:321:20)
api | at Connection.EventEmitter.emit (domain.js:485:12)
api | at Socket.<anonymous> (/api/node_modules/pg/lib/connection.js:139:10)
api | at Socket.emit (events.js:333:22)
api | at Socket.EventEmitter.emit (domain.js:485:12)
api | at endReadableNT (_stream_readable.js:1201:12)
api | at processTicksAndRejections (internal/process/task_queues.js:84:21)
api | [nodemon] app crashed - waiting for file changes before starting...
Dockerfile
FROM node
WORKDIR /api
ADD . /api
RUN npm install
RUN npm install -g nodemon
EXPOSE 3000
docker -compose.yml
version: "3.7"
services:
api:
container_name: api
build: .
volumes:
- .:/api
ports:
- "3000:3000"
restart: unless-stopped
working_dir: /api
command: nodemon server.js
сервер. js
...
try {
let pgDbConfig;
pgDbConfig = {
host: '<host>',
user: '<dbuser>',
password: '<dbpass>',
port: 5432,
database: '<dbname>',
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000
};
pgPool = new Pool(pgDbConfig);
// the pool will emit an error on behalf of any idle clients it contains if a backend error or network partition happens
pgPool.on('error', (err, client) => {
console.error('Unexpected error on idle pg client', err, client);
});
// trying to use error listener after the connect
pgPool.on('connect', () => {
pgPool.on('error', err => console.log(err));
});
console.log('API is ready');
} catch(exception) {
console.error('API is not ready');
process.exit(1);
}
...