docker-compose connection between node and mongo - node.js

I read a lots of examples about connecting apps with docker, it seems really simple
Im my case I have
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
service:
build:
context: .
container_name: service
restart: always
ports:
- 3012:3012
depends_on:
- mongodb
links:
- mongodb
My connection in node is
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://admin:admin#mongodb:27017/admin?replicaSet=rs0&slaveOk=true'
MongoClient.connect(mongoUrl, { useNewUrlParser: true }, (err, client) => {
console.log(err)
});
I have the following error
{ MongoNetworkError: failed to connect to server [9d574801e4b4:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND 9d574801e4b4 9d574801e4b4:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
I do not understand why the host become 9d574801e4b4
When I run ping mongodb into my container everything is fine
PING mongodb (172.21.0.3) 56(84) bytes of data.
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=3 ttl=64 time=0.102 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=4 ttl=64 time=0.216 ms
^C
--- mongodb ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3156ms
rtt min/avg/max/mdev = 0.088/0.126/0.216/0.053 ms

By default mongo listens on localhost only. If it is for local development, simply bind it to all interfaces:
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
command: --bind_ip_all

Just a guess here, but the depends_on field in Docker Compose only means "this container has to wait until the other one is started." The service inside doesn't necessarily have to be up and running yet. So your application may be starting too quickly. The Mongo server may not be ready to accept connections yet when your app tries to connect. Run docker-compose up, wait for it to fail to start, and then run docker-compose restart service.
If this fixes it, then you will probably want to make your application retry connecting after some sort of timeout if it fails to connect on start up.

Check this out:
docker-compose.yaml
version: '2'
services:
nodejs:
container_name: nodejs
image: nexus.XXXX.com:8000/${BE_BUILD}
restart: always
environment:
- dbhost=mongo
ports:
- "8888:8888"
links:
- mongo
mongo:
container_name: mongo
image: nexus.XXXX.com:8000/doc/docker/proj-mongodb:latest
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- "27017:27017"
And I am successfully able to connect by below URL:
const mongoUrl = mongodb://admin:admin#mongo:27017/admin?authSource=admin
Check below things that can solve the issue:
Set dbHost name from NodeJS configuration.
Link mongo container from NodeJS configuration.
Use container name to connect.
Check if authSource require to the URL.

Just find the issue I do not really understand how does it works ...
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
networks:
service-network:
aliases:
- localhost
service:
build:
context: .
container_name: service
restart: always
ports:
- 3012:3012
depends_on:
- mongodb
links:
- mongodb
networks:
app-admin-network:
driver: bridge

Can't see the docker-compose up log but. had similar problem today.
Solved with a small script I have made.
wait-for-mongo
In your dockerfile add this:
RUN npm install -g wait-for-mongodb-slim
## Launch the wait tool and then your application
CMD wait-for-mongo --uri $MONGO_URI --t 2.5 && npm start
where --t is the sceonds until it checks again.
and on your compose add the uri as an environment variable:
environment:
MONGO_URI: mongodb://usr:pw#containername:27017/dbname

Related

Can't reach postgres container from app container

I have a docker-composer.yml that is setting up two services: server and db. The Node.js server, which is the server service, uses pg to connect to the PostgreSQL database; and the db service is a PostgreSQL image.
On the server startup, it tries to connect to the database but gets a timeout.
docker-compose.yml
version: '3.8'
services:
server:
image: myapi
build: .
container_name: server
env_file: .env
environment:
- PORT=80
- DATABASE_URL=postgres://postgres:postgres#db:15432/mydb
- REDIS_URL=redis://redis
ports:
- 3000:80
depends_on:
- db
command: node script.js
restart: unless-stopped
db:
image: postgres
container_name: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mydb
ports:
- 15432:15432
volumes:
- db-data:/var/lib/postgresql/data
command: -p 15432
restart: unless-stopped
volumes:
db-data:
Edit: code above changed to remove links and expose.
db service output:
db |
db | PostgreSQL Database directory appears to contain a database; Skipping initialization
db |
db | 2020-11-05 20:18:15.865 UTC [1] LOG: starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db | 2020-11-05 20:18:15.865 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 15432
db | 2020-11-05 20:18:15.865 UTC [1] LOG: listening on IPv6 address "::", port 15432
db | 2020-11-05 20:18:15.873 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.15432"
db | 2020-11-05 20:18:15.880 UTC [25] LOG: database system was shut down at 2020-11-05 20:18:12 UTC
db | 2020-11-05 20:18:15.884 UTC [1] LOG: database system is ready to accept connections
script.js - used by the command from the server service.
const pg = require('pg');
console.log(process.env.DATABASE_URL);
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
connectionTimeoutMillis: 5000,
});
pool.connect((err, _, done) => {
if (err) {
console.error(err);
done(err);
}
done();
});
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res);
pool.end();
});
const client = new pg.Client({
connectionString: process.env.DATABASE_URL,
connectionTimeoutMillis: 5000,
});
client.connect(console.error);
client.query('SELECT NOW()', (err, res) => {
console.log(err, res);
client.end();
});
server service output:
NOTE: The first line is the output of the first console.log call from script.js.
NOTE: Since the server service is set up with restart: unless-stopped, it will repeat this output forever.
server | postgres://postgres:postgres#db:15432/mydb
server | Error: Connection terminated due to connection timeout
server | at Connection.<anonymous> (/home/node/app/node_modules/pg/lib/client.js:255:9)
server | at Object.onceWrapper (events.js:421:28)
server | at Connection.emit (events.js:315:20)
server | at Socket.<anonymous> (/home/node/app/node_modules/pg/lib/connection.js:78:10)
server | at Socket.emit (events.js:315:20)
server | at emitCloseNT (net.js:1659:8)
server | at processTicksAndRejections (internal/process/task_queues.js:79:21)
server | at runNextTicks (internal/process/task_queues.js:62:3)
server | at listOnTimeout (internal/timers.js:523:9)
server | at processTimers (internal/timers.js:497:7)
server | Error: Connection terminated due to connection timeout
server | at Connection.<anonymous> (/home/node/app/node_modules/pg/lib/client.js:255:9)
server | at Object.onceWrapper (events.js:421:28)
server | at Connection.emit (events.js:315:20)
server | at Socket.<anonymous> (/home/node/app/node_modules/pg/lib/connection.js:78:10)
server | at Socket.emit (events.js:315:20)
server | at emitCloseNT (net.js:1659:8)
server | at processTicksAndRejections (internal/process/task_queues.js:79:21)
server | at runNextTicks (internal/process/task_queues.js:62:3)
server | at listOnTimeout (internal/timers.js:523:9)
server | at processTimers (internal/timers.js:497:7) undefined
server | Error: timeout expired
server | at Timeout._onTimeout (/home/node/app/node_modules/pg/lib/client.js:95:26)
server | at listOnTimeout (internal/timers.js:554:17)
server | at processTimers (internal/timers.js:497:7)
server | Error: Connection terminated unexpectedly
server | at Connection.<anonymous> (/home/node/app/node_modules/pg/lib/client.js:255:9)
server | at Object.onceWrapper (events.js:421:28)
server | at Connection.emit (events.js:315:20)
server | at Socket.<anonymous> (/home/node/app/node_modules/pg/lib/connection.js:78:10)
server | at Socket.emit (events.js:315:20)
server | at emitCloseNT (net.js:1659:8)
server | at processTicksAndRejections (internal/process/task_queues.js:79:21) undefined
server | postgres://postgres:postgres#db:15432/mydb
...
From the host computer, I can reach the PostgreSQL database at the db service, connecting successfully, using the same script from the server service.
The output of the script running from the host computer:
➜ node script.js
postgres://postgres:postgres#localhost:15432/mydb
null Client { ... }
undefined Result { ... }
null Result { ... }
This output means the connection succeeded.
In summary:
I can't reach the db container from the server container, getting timeouts on the connection, but I can reach the db container from the host computer, connecting successfully.
Considerations
First, thanks for the answer so far. Addressing some points raised:
Missing network:
It isn't required because docker-compose has a default network. A tested with a custom network but it didn't work either.
Order of initialization:
I'm using depends_on to ensure the db container is started first but I know it isn't ensuring the database is in fact initialized first then the server. It isn't the problem because the server breaks when a timeout happens and it runs again because it is set up with restart: unless-stopped. So if the database is still initializing on the first or second try to start the server, there is no problem because the server will continue to be restarted until it succeeds in the connection (which never happened.)
UPDATE:
From the server container, I could reach the database at the db service using psql. I still can't connect from the Node.js app there.
The DATABASE_URL isn't the problem because the URI I used in the psql command is the same URI used by the script.js and printed by the first console.log call there.
Command-line used:
docker exec -it server psql postgres://postgres:postgres#db:15432/mydb
Edit: Improved code by removing the dependency for Sequelize. Now it uses only pg and calls the script directly.
Thanks for providing the source to reproduce the issue.
No issues in the docker-compose file as you have already ruled out.
The problem lies between your Dockerfile and the version of node-pg that you are using.
You are using node:14-alpine and pg: 7.18.2.
Turns out there is a bug on node 14 and earlier versions of node-pg.
Solution is either downgrade to node v12 or use latest version of node-pg which is currently 8.4.2 (fix went in on v8.0.3).
I have verified both these solutions on the branch you provided and they work.
This isn't a complete answer; I don't have your code handy so I can't actually test the compose file. However, there are a few issues there I'd like to point out:
The links directive is deprecated.
The links is a legacy option that was used before Docker introduced user-defined networks and automatic DNS support. You can just get rid of it. Containers in a compose file are able to refer to each other by name without.
The expose directive does nothing. It can be informative in for example a Dockerfile as a way of saying, "this image will expose a service on this port", but it doesn't actually make anything happen. It's almost entirely useless in a compose file.
The depends_on directive is also less useful than you would think. It will indeed cause docker-compose to bring up the database container first, but it the container is considered "up" as soon as the first process has started. It doesn't cause docker-compose to wait for the database to be ready to service requests, which means you'll still run into errors if your application tries to connect before the database is ready.
The best solution to this is to built database re-connection logic into your application so that if the database ever goes down (e.g. you restart the postgres container to activate a new configuration or upgrade the postgres version), the app will retry connections until it is successful.
An acceptable solution is to include code in your application startup that blocks until the database is responding to requests.
The problem has nothing to do with docker. To test that, perform following actions :
By using this docker-compose.yml file:
version: '3.8'
services:
app:
image: ubuntu
container_name: app
command: sleep 8h
db:
image: postgres
container_name: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mydb
expose:
- '15432'
ports:
- 15432:15432
volumes:
- db-data:/var/lib/postgresql/data
command: -p 15432
restart: unless-stopped
volumes:
db-data:
Perform a docker exec -it app bash to go into container app then install postgresql-client with apt install -y postgresql-client`.
Command psql -h db -p 15432 -U postgres -W succeeded !
Check pg configuration
You say that pg use environment variable DATABASE_URL to reach postgresql. I'm not sure :
From https://node-postgres.com/features/connecting, we can found this example :
$ PGUSER=dbuser \
PGHOST=database.server.com \
PGPASSWORD=secretpassword \
PGDATABASE=mydb \
PGPORT=3211 \
node script.js
And this sentence :
node-postgres uses the same environment variables as libpq to connect to a PostgreSQL server.
In libpq documentation, no DATABASE_URL.
To adapt example provided in pg documentation with your docker-compose.yml file, try with following file (I only changed environments variables of app service) :
version: '3.8'
services:
server:
image: myapi
build: .
container_name: server
env_file: .env
environment:
- PORT=80
- PGUSER=postgres
- PGPASSWORD=postgres
- PGHOST=db
- PGDATABASE=mydb
- PGPORT=15432
- REDIS_URL=redis://redis
ports:
- 3000:80
depends_on:
- db
command: node script.js
restart: unless-stopped
db:
image: postgres
container_name: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mydb
ports:
- 15432:15432
volumes:
- db-data:/var/lib/postgresql/data
command: -p 15432
restart: unless-stopped
volumes:
db-data:

Can not access replicaset mongodb(docker) from host

I have my nodejs code running on my host machine(MACOS) and which is trying to connect to the mongodb replicaset which is running in the docker.
version: "3"
services:
redis_master:
image: redis:2.8
volumes:
- "/Users/hiteshbaldaniya/docker-redis/master:/data/"
ports:
- "6379:6379"
networks:
- database
mongodb_primary:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27017/" --port 27017
ports:
- "27017:27017"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database
mongodb_secondary1:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27018/" --port 27018
ports:
- "27018:27018"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database
mongodb_secondary2:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27019/" --port 27019
ports:
- "27019:27019"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database
hdb_nginx:
build:
context: ./nginx/
dockerfile: DockerfileNginx.dev
ports:
- "8081:80"
volumes:
- "/Users/hiteshbaldaniya/logs/docker-nginx/:/var/log/nginx/"
networks:
- backend
networks:
backend:
driver: bridge
database:
driver: bridge
All 3 ports are open on my host machine and I tried using telnet and I am able to connect to all the ports as well.
My nodejs application using mongodb-node-driver and using following configuration.
module.exports = {
servers: [{
host: 'localhost',
port: 27017,
},
{
host: 'localhost',
port: 27018,
},
{
host: 'localhost',
port: 27019,
},
],
database: 'mydatabase',
options: {
"raw": false,
"poolSize": 5,
"readPreference": "primaryPreferred",
"w": 1,
"wtimeout": 12000,
"replicaSet": "hdbrs"
}
};
While connecting to mongodb my application throws following error can someone help me over here?
{ MongoNetworkError: failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]
at Pool.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/topologies/server.js:505:11)
at Pool.emit (events.js:198:13)
at Connection.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/pool.js:329:12)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Socket.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
name: 'MongoNetworkError',
message:
'failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]' }
thanks,
I was able to figure out solution. By adding the host entries in my /etc/hosts file.
127.0.0.1 mongodb_primary
127.0.0.1 mongodb_secondary1
127.0.0.1 mongodb_secondary2
I am still not sure what is wrong with localhost while connecting to mongodb running inside docker from host machine, I would still need to understand the behaviour of this.
Just for the node I have also tried adding the --bind_ip_all in command line during command for mongodb run.
Do let me know if someone know it. thanks

Node.js LoopBack framework, docker-compose.yml, MongoDB Connector - Error: MongoServerSelectionError: connect ECONNREFUSED

I can't connect MongoDB to my LoopBack framework.
I can connect into database with Mongo Express. I can create db and collections. But it's running on localhost. From my app i need to connect with mongo:27017.
docker-compose.yml
version: "3.7"
services:
web:
build:
context: .
dockerfile: .docker/node/Dockerfile
volumes:
- .:/home/node/app
ports:
- 3000:3000
depends_on:
- mongo
links:
- mongo
mongo:
image: mongo:latest
restart: always
volumes:
- ./src/datasources/mongodb:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
Dockerfile
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim
# Set to a non-root built-in user `node`
USER node
# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY --chown=node package*.json ./
RUN npm install
# Bundle app source code
COPY --chown=node . /home/node/app
RUN npm run build
# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000
EXPOSE ${PORT}
CMD [ "node", "." ]
LoopBack datasources - mongo-db.datasource.config.json
{
"name": "MongoDB",
"connector": "mongodb",
"url": "",
"host": "mongo",
"port": 27017,
"user": "root",
"password": "sportee",
"database": "sportee",
"useNewUrlParser": true
}
docker error
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:430:30)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
Emitted 'error' event at:
at MongoDbDataSource.postInit (/home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:502:16)
at onError (/home/node/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:316:21)
at /home/node/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:324:9
at /home/node/app/node_modules/mongodb/lib/utils.js:722:9
at err (/home/node/app/node_modules/mongodb/lib/mongo_client.js:216:23)
at connectCallback (/home/node/app/node_modules/mongodb/lib/operations/connect.js:350:5)
at topology.connect.err (/home/node/app/node_modules/mongodb/lib/operations/connect.js:583:14)
at Object.selectServer.err [as callback] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:285:11)
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:435:25)
[... lines matching original stack trace ...]
Unhandled error in GET /users: 500 Error: Timeout in connecting after 5000 ms
at Timeout._onTimeout (/home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:2640:10)
at /home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:343:12
Can someone help please? :)
UPDATE:
I resolve my problem with adding networks into my docker-compose.yml
networks:
app-tier:
driver: bridge
me-tier:
driver: bridge

Postgres ECONNREFUSED on Docker Compose with NodeJS [duplicate]

This question already has answers here:
ECONNREFUSED for Postgres on nodeJS with dockers
(7 answers)
Closed 2 years ago.
I get an ECONNREFUSED when trying to connect to a postgres server in docker from a NodeJS app in docker when running both via docker-compose. However I can connect from my host machine. Here is my docker-compose.yml:
version: "2.4"
services:
api:
build:
context: .
target: dev
depends_on:
- postgres
ports:
- "8080:8080"
- "9229:9229"
networks:
- backend
environment:
- NODE_ENV=development
- PGHOST=postgres
- PGPASSWORD=12345678
- PGUSER=test
- PGDATABASE=test
- PGPORT=5433
volumes:
- .:/node/app
- /node/app/node_modules # Use empty volume to hide the node_modules from the host os
postgres:
image: postgres:11
restart: always
ports:
- "5433:5432"
networks:
- backend
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: 12345678
POSTGRES_USER: test
POSTGRES_DB: test
networks:
backend:
volumes:
db-data:
The nodeJS code:
const client = new Client({
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
password: process.env.PGPASSWORD,
port: Number(process.env.PGPORT),
});
client.connect();
The error:
{ Error: connect ECONNREFUSED 172.22.0.2:5433
api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
api_1 | errno: 'ECONNREFUSED',
api_1 | code: 'ECONNREFUSED',
api_1 | syscall: 'connect',
api_1 | address: '172.22.0.2',
api_1 | port: 5433 }
At the same time I can connect from the host OS to the database server without any problems. Is there any problems with networking?
Edit: The dB server is ready to accept connections before the nodejs app tries that (I also tried with retrying the connection from within the nodejs app).
No, there is nothing wrong with networking. Just because you're connecting on the wrong port.
Inside compose network, your postgres container exposed 5432 port so it only accept the request via that port inside the compose network. So just need to change PGPORT=5433 to PGPORT=5432.
The reason why you can access from your host OS is because docker-compose mapped your port 5433:5432 so all request to 5433 from outside (host OS) will be pass to 5432 inside your compose network.
Hope that clear enough for you to solve the issue.

ECONNREFUSED when trying to connect to redis via node using docker-compose

I'm trying to switch over to docker, for some reason my Node.js application won't connect to redis running on docker.
This is my docker compose:
version: "3"
services:
chatty-backend:
container_name: chatty-backend
build: ./
volumes:
- ./:/usr/src/chatty-backend
command: npm start
working_dir: /usr/src/chatty-backend
ports:
- "5000:5000"
links:
- redis
- mongo
depends_on:
- redis
- mongo
mongo:
image: mongo:bionic
ports:
- "27017:27017"
redis:
image: redis
ports:
- "6379:6379"
command:
redis-server
This is where I'm connecting to redis:
import * as Redis from 'ioredis';
const redis = new Redis({ host: 'redis', port: 6379 });
export default redis;
Also tried new Redis('redis://redis:6379')
I'm getting this error currently:
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
The issue caused by graphql-redis-subscriptions.
I forgot to pass the updated confifg.

Resources