nodejs container won't connect to redis container docker-compose - node.js

I have an express API running in a node container and a redis container.
When attempting to connect the node container to redis I am greeted with the following error.
api_1 | events.js:291
api_1 | throw er; // Unhandled 'error' event
api_1 | ^
api_1 |
api_1 | Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
api_1 | Emitted 'error' event on RedisAdapter instance at:
api_1 | at RedisClient.onError (/usr/src/api/node_modules/socket.io-redis/dist/index.js:65:22)
api_1 | at RedisClient.emit (events.js:314:20)
api_1 | at RedisClient.on_error (/usr/src/api/node_modules/redis/index.js:342:14)
api_1 | at Socket.<anonymous> (/usr/src/api/node_modules/redis/index.js:223:14)
api_1 | at Socket.emit (events.js:314:20)
api_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
api_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
api_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
api_1 | errno: 'ECONNREFUSED',
api_1 | code: 'ECONNREFUSED',
api_1 | syscall: 'connect',
api_1 | address: '127.0.0.1',
api_1 | port: 6379
api_1 | }
Here the code that is establishing the connection:
const redisClient = require("redis").createClient({host: process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost'});
I have tried changing 'localhost' to "redis" to "redis://redis:6379". Nothing works.
As I understand it "localhost" in the context of a container refers to its own internal network, however both nodejs and redis are running on the same network which I've specified in my docker compose file.
Docker compose snippet
api:
build: ./api
ports:
- "3004:3004"
volumes:
- type: bind
source: ./api
target: /usr/src/api
working_dir: /usr/src/api
depends_on:
- redis
networks:
- backend
redis:
image: "redis:latest"
ports:
- "6379:6379"
hostname: redis
networks:
- backend
```
What could this be. All the "solutions" that I've managed to find through research don't work for me.

Just to close the question, when trying to connect to localhost inside the container it assumes the container itself and because the node.js container doesnt publish on the redis port the connection is refused. On the other hand because using docker-compose, you get a default docker network binding them together so using the container\service names as internal secured DNS records works.
for a more detailed explenation of the docker networking inner works see this thread

Related

dns in Dockercontainer not working with vue.js and node: Error: getaddrinfo ENOTFOUND db

I created a vue.js app which I know try to dockerize.
As Backend I'm using node.js for api calls, which is using mysql as a database.
So I try to create an ui container, a db container, and also a container for nginx.
When trying to create these via docker-compose up --build, I get the following error.
Does anybody know what is the problem here?
$ vue-cli-service build
- Building for production...
events.js:187
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND db
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
--------------------
at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
[...]
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'db',
fatal: true
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: Service 'ui' failed to build: The command '/bin/sh -c yarn run build' returned a non-zero code: 1
This is my docker-compose.yml
version: '3'
networks:
app-tier:
services:
db:
image: mysql:5.7
volumes:
- ./sql:/docker-entrypoint-initdb.d
restart: always
networks:
- app-tier
environment:
- MYSQL_USER=user
[...]
ports:
- 3306:3306
ui:
build: ./ui
container_name: ui
restart: always
networks:
- app-tier
expose:
- 80
ports:
- 3000:3000
[...]
This .ui/Dockerfile
# build stage
FROM node:12.14.0-alpine as build-stage
[...]
# RUN yarn server
RUN yarn run build
# Production stage
FROM nginx:1.16.1-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
[...]
This is ./ui/src/server/db.js:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'db',
user: 'root',
password: 'password',
database: 'dbname',
port: '3306'
});
connection.connect();
module.exports = connection;

Docker container connection to host's Kafka throws : Error: connect ECONNREFUSED 127.0.0.1:9092

I have a nodejs app containerized as a linux container which uses the kafka-node library.
Kafka runs on the host machine which runs windows with:
Zookeeper port : 2181
Kafka broker port : 9092
I run the the nodejs container with the following command:
docker container run --network host --name nm name:1.0
In order to connect with the host's kafka I am using the following command:
client = new kafka.KafkaClient({kafkaHost: "localhost:9092"});
But this throws :
Error: connect ECONNREFUSED 127.0.0.1:9092
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1126:14) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 9092
}
When I change the connect command to :
client = new kafka.KafkaClient({kafkaHost: "host.docker.internal:9092"});
I am getting :
TimeoutError: Request timed out after 30000ms
at new TimeoutError (/usr/src/app/node_modules/kafka-node/lib/errors/TimeoutError.js:6:9)
at Timeout._onTimeout (/usr/src/app/node_modules/kafka-node/lib/kafkaClient.js:491:14)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
message: 'Request timed out after 30000ms'
}
Can someone advise what I am doing wrong ?
UPDATE
When switching to a linux host machine, the above localhost methodology runs just fine .
Same problem here, but I can solve
You must set the environment variable KAFKA_ADVERTISED_HOST_NAME with your domain host.docker.internal:9092 in Kafka broker
My example:
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
hostname: 'kafka-internal.io'
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka-internal.io
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
Now I can connect inside container using kafka-internal.io:9092 :)
Not the best answer but switching to a Linux host should make the first methodology (localhost:IP) run just fine .

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 error in docker-compose with NodeJS and postgresql in google cloud

I have created my react app with Node.js and postgresql and I deployed in google cloud. I created a docker image of postgres and nodejs and I uploaded images to docker hub. From gcloud I accessing Those images.
This is my docker-compose-production.yml file.
version: '2.0'
services:
postgres:
image : mycompany/myapp:pglatest
restart : always
volumes:
- ./backdata/databackups:/var/lib/postgresql/backdata
ports:
- "5433:5432"
backend:
image: mycompany/myapp:nodelatest7
command: npm run start
ports:
- "5001:5000"
depends_on:
- postgres
environment:
POSTGRES_URL: postgresql://postgres:root#postgres:5432/db_mydb
DEBUG: hms-backend:*
when I run command
sudo docker-compose -f docker-compose-production.yml up --build -d
2 images are created.
after that I have run tail command
sudo docker-compose -f docker-compose-production.yml logs -t backend
I'm getting error as
backend_1 | 2018-09-15T09:12:18.926001351Z REST API listening on port 5000
backend_1 | 2018-09-15T09:12:18.937246598Z error { Error: connect ECONNREFUSED 192.168.80.2:5432
backend_1 | 2018-09-15T09:12:18.937268668Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
backend_1 | 2018-09-15T09:12:18.937280934Z errno: 'ECONNREFUSED',
backend_1 | 2018-09-15T09:12:18.937283960Z code: 'ECONNREFUSED',
backend_1 | 2018-09-15T09:12:18.937286817Z syscall: 'connect',
backend_1 | 2018-09-15T09:12:18.937289488Z address: '192.168.80.2',
backend_1 | 2018-09-15T09:12:18.937292260Z port: 5432 }
How to solve this problem
For me your postgres url is wrong : postgresql://postgres:root#postgres:5432/db_mydb
It should be postgresql://postgres:root#postgres:5433/db_mydb since the postgres "exposed" port in 5433
Hum, by i think you should add "container_name" in the docker-compose
services:
postgres:
container_name: my_postgres
and use this name for the "adress" of your postgres
postgresql://my_postgres:root#postgres:5433/db_mydb

Postgres Shutdown Immediately after docker compose up?

Here I am using postgresql image and node official image and
my docker-compose.yml file looks like
version: '2'
services:
postgres:
restart: always
image: sameersbn/postgresql:9.6-2
ports:
- "5432:5432"
environment:
- DB_USER=shorturl
- DB_PASS=shorturl
- DB_NAME=shorturl
web:
build: .
ports:
- "4000:4000"
volumes:
- .:/shortlr
depends_on:
- postgres
command: ["./wait-for-it.sh","postgres:5432", "--", "npm", "start"]
and when i run docker-compose up then my logs looks like
shubham#shuboy2014:~/shortlr$ docker-compose up
Recreating shortlr_postgres_1 ...
Recreating shortlr_postgres_1 ... done
Recreating shortlr_web_1 ...
Recreating shortlr_web_1 ... done
Attaching to shortlr_postgres_1, shortlr_web_1
postgres_1 | Initializing datadir...
web_1 | wait-for-it.sh: waiting 15 seconds for postgres:5432
postgres_1 | Initializing certdir...
postgres_1 | Initializing logdir...
postgres_1 | Initializing rundir...
postgres_1 | Setting resolv.conf ACLs...
postgres_1 | Creating database user: shorturl
postgres_1 | Creating database: shorturl...
postgres_1 | ‣ Granting access to shorturl user...
postgres_1 | Starting PostgreSQL 9.6...
postgres_1 | LOG: database system was shut down at 2017-04-28 14:23:20 UTC
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: autovacuum launcher started
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: incomplete startup packet
web_1 | wait-for-it.sh: postgres:5432 is available after 2 seconds
web_1 | npm info it worked if it ends with ok
web_1 | npm info using npm#4.2.0
web_1 | npm info using node#v7.9.0
web_1 | npm info lifecycle shortlr#0.0.5~prestart: shortlr#0.0.5
web_1 | npm info lifecycle shortlr#0.0.5~start: shortlr#0.0.5
web_1 |
web_1 | > shortlr#0.0.5 start /shortlr
web_1 | > node server.js
web_1 |
web_1 | Listening on http://localhost:4000/
web_1 | Unhandled rejection SequelizeBaseError: connect ECONNREFUSED 127.0.0.1:5432
web_1 | at /shortlr/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20
web_1 | at Connection.<anonymous> (/shortlr/node_modules/pg/lib/client.js:186:5)
web_1 | at emitOne (events.js:96:13)
web_1 | at Connection.emit (events.js:191:7)
web_1 | at Socket.<anonymous> (/shortlr/node_modules/pg/lib/connection.js:86:10)
web_1 | at emitOne (events.js:96:13)
web_1 | at Socket.emit (events.js:191:7)
web_1 | at emitErrorNT (net.js:1283:8)
web_1 | at _combinedTickCallback (internal/process/next_tick.js:80:11)
web_1 | at process._tickCallback (internal/process/next_tick.js:104:9)
postgresql shutdown immediately when i run docker-compose up and any helpful answers will be appreciated.
shubham#shuboy2014:~/shortlr$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
shortlr_postgres_1 /sbin/entrypoint.sh Up 0.0.0.0:5432->5432/tcp
shortlr_web_1 ./wait-for-it.sh postgres: ... Up 0.0.0.0:4000->4000/tcp
Add a link between your web and postgres image like so:
version: '2'
services:
postgres:
restart: always
image: sameersbn/postgresql:9.6-2
ports:
- "5432:5432"
environment:
- DB_USER=shorturl
- DB_PASS=shorturl
- DB_NAME=shorturl
web:
build: .
ports:
- "4000:4000"
volumes:
- .:/shortlr
depends_on:
- postgres
links:
- postgres
command: ["./wait-for-it.sh","postgres:5432", "--", "npm", "start"]
Although you should use networks instead because links is a legacy option. Read the docs on networks here

Resources