docker-compose node app can't reach mongo - node.js

I'm having a weird connectivity issue with my docker containers.
This is my compose.yaml
version: "3.3"
services:
node-app:
image: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}"
container_name: "node-app"
depends_on:
- mongo
env_file: ./config/.env
ports:
- 43332:43332
networks:
- app-network
environment:
WAIT_HOSTS: mongo:27017
mongo:
container_name: "app-mongo"
image: mongo:4.0
volumes:
- /data/db:/data/db
ports:
- 27017:27017
networks:
- app-network
networks:
app-network:
So I'm trying to set up a CI/CD pipeline and the first thing it does is to run tests when code is pushed. The pipeline runs this command docker-compose run --rm app-node npm run test.
However the it keeps getting the following error.
MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: connection 0 to mongo:27017 timed out]
My app is connecting to mongo like this
mongoose.connect('mongodb://mongo:27017', { useNewUrlParser: true });
mongoose.Promise = global.Promise;
mongoose.connection.on('error', error => {
logMe(error);
process.exit(1);
});
The weird thing is I can see that the app container itself can talk to the mongo conatiner by its service name ('mongo') at the OS level.
So if I run this - docker-compose run --rm app-node ping -c 1 mongo, the ping is successful.
It's running under the following environment
$ docker -v
Docker version 18.09.7, build 2d0083d
$ docker-compose version
docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018
What could I be doing wrong?? Any help is appreciated.

Related

nodejs- 'failed to connect to server [mongo:27017] on first connect'

I was following a youtube tutorial on docker-compose https://youtu.be/hP77Rua1E0c
when I try to run this code as it is It show error as
failed to connect to server [mongo:27017] on first connect [MongoNetworkError: connection 0 to mongo:27017 timed out]'
I am running this file on docker-compose
github repo
this is my docker-compose.yml
version: '3'
services:enter image description here
app:
container_name: docker-node-mongo
restart: always
build: .
ports:
- '80:3000'
links:
- mongo
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
I'm using mongo in the URL
mongoose
.connect(
'mongodb://mongo:27017/docker-node-mongo',
{ useNewUrlParser: true }
)
I am using ubuntu 22.04 LTS.
I'm getting this error
error
I am new to docker-compose, Please help me out.

Node-postgres hangs inside docker container when running in ubuntu server

I have an application running on several containers and I am using docker-compose to run these services inside containers. I am using NodeJS with postgresql using node-postgresql library(pg).
When I run the application in my local machine it works perfectly fine. And also when I build the application in a aws e2 ubuntu server , it builds without any error. And when I try to check the endpoints it works pretty much well. But when I try an endpoint that involves a database call, no response is received. I tried to debug the application by adding some console logs and I think the problem is with the pg library. When the code reaches pool.query it just hangs there without giving any error. I tried to console.log , try catch errors but everytime the code stops at a pool.query() and it hangs. Any idea why this is happening ? Tried for hours to find a solution online but no luck.
I found another similar issue here https://github.com/brianc/node-postgres/issues/2069
My docker-compose.yml
version: '3'
services:
postgres:
container_name: postgres
image: mdillon/postgis
environment:
POSTGRES_MULTIPLE_DATABASES: user_data,gov_auth
POSTGRES_USER: commhawk
POSTGRES_PASSWORD: password
volumes:
- postgres:/data/postgres
- ./database:/docker-entrypoint-initdb.d
ports:
- "5433:5432"
restart: unless-stopped
rethinkdb:
container_name: rethinkdb
image: rethinkdb:latest
ports:
- "3004:8080"
- "29015:29015"
- "28015:28015"
volumes:
- rethinkdb:/data
user_data_service:
build: './user_data_service'
container_name: uds
ports:
- "3002:3000"
depends_on:
- postgres
environment:
DATABASE_URL: postgres://commhawk:password#postgres:5432/user_data
# volumes:
# - ./user_data_service:/src
# - container_node_modules:/src/node_modules
gov_authority_service:
build: './gov_authority_service'
container_name: gov_auth
ports:
- "3001:3000"
depends_on:
- postgres
environment:
DATABASE_URL: postgres://commhawk:password#postgres:5432/gov_auth
# volumes:
# - ./gov_authority_service:/src
socket_service:
build: './socket_service'
container_name: socket
ports:
- "3003:3000"
depends_on:
- rethinkdb
# volumes:
# - ./socket_service:/src
api_gateway:
image: express-gateway:latest
ports:
- "8080:8080"
depends_on:
- user_data_service
- socket_service
- gov_authority_service
volumes:
- ./api_gateway/gateway.config.yml:/var/lib/eg/gateway.config.yml
volumes:
postgres:
rethinkdb:
# container_node_modules:
Simply when I run like following, it stops without any result. When I include that inside try catch, no error is shown also. No way to deubug
let result = await pool.query("SELECT NOW()")
console.log(result)
Node-postgresql connection
const {Pool} = require("pg");
const pool = new Pool({
user: "commhawk",
host: "postgres",
database: "gov_auth",
password: "password",
port: "5432"
});
module.exports = pool;
Tried it with connectionString also. Still no luck !!
Can provide more info if needed. Thanks in advance
UPDATE (SOLUTION)
Okay so I figured out the error. When I built it on my local machine, it is using nodejs 13 and pg 8.0.2. But when I tried to built it on my server(like 1 month after building on the local), since I am using node:latest in my Dockerfile, it pulls the latest version which is nodejs 14. Nodejs 14 is incompatible with pg 8.0.2. So that is why the local versions runs well and the server version fails.
Solutions:
Downgrade nodejs to 13 to use with pg 8.0.2
Upgrade pg to 8.0.3 to use with nodejs 14

Dockerized MongoDB Failing to connect on Heroku from NodeJs Container

i have a node web service and I'm a bit new to docker, so please bear with me. Below is my docker-compose file. I have run this image locally and it works fine, but when trying to deploy it to Heroku I get the following error:
{ MongoError: failed to connect to server [mongo:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongo mongo:27017]
I've found where I'm supposed to set the environment flags in Heroku, so I managed to check that and its fine. But it still can't find mongo:27017
What could I be doing wrong?
version: "3"
services:
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/charecters
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
mongo-seed:
build: ./mongo-seed
links:
- mongo
volumes:
- ./mongo-seed:/mongo-seed
command:
- /mongo-seed/script.sh
app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
environment:
- DB_SERVERS=mongo:27017
links:
- mongo
is depreciated try
depends_on:
- mongo
also, check that
mongo:
container_name: mongo, is properly referenced in DB setup
change command:
- /mongo-seed/script.sh
try command:
- 'node your-app-file'

Docker and Rabbitmq: ECONNREFUSED between containers

I am trying to setup separate docker containers for rabbitmq and the consumer for the container, i.e., the process that would listen on the queue and perform the necessary tasks. I created the yml file, and the docker file.
I am able to run the yml file, however when I check the docker-compose logs I see where there are ECONNREFUSED errors.
NewUserNotification.js:
require('seneca')()
.use('seneca-amqp-transport')
.add('action:new_user_notification’, function(message, done) {
…
return done(null, {
pid: process.pid,
status: `Process ${process.pid} status: OK`
})
.listen({
type: 'amqp',
pin: ['action:new_user_notification’],
name: 'seneca.new_user_notification.queue',
url: process.env.AMQP_RECEIVE_URL,
timeout: 99999
});
error message in docker-compose log:
{"notice":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.","code":
"act_execute","err":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1",
"port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"act_execute","syscall":"connect","address":"127.0.0.1",
"port":5672,"eraro":true,"orig":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1",
"port":5672},"isOperational":true,"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},
"seneca":true,"package":"seneca","msg":"seneca: Action hook:listen,role:transport,type:amqp failed: connect ECONNREFUSED 127.0.0.1:5672.",
"details":{"message":"connect ECONNREFUSED 127.0.0.1:5672","pattern":"hook:listen,role:transport,type:amqp","instance":"Seneca/…………/…………/1/3.4.3/-“,
”orig$":{"cause":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672},"isOperational":true,
"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5672}
sample docker-compose.yml file:
version: '2.1'
services:
rabbitmq:
container_name: "4340_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- 15672:15672
- 15671:15671
- 5672:5672
volumes:
- /rabbitmq/lib:/var/lib/rabbitmq
- /rabbitmq/log:/var/log/rabbitmq
- /rabbitmq/conf:/etc/rabbitmq/
account:
container_name: "account"
build:
context: .
dockerfile: ./Account/Dockerfile
ports:
- 3000:3000
links:
- "mongo"
- "rabbitmq"
depends_on:
- "mongo"
- "rabbitmq"
new_user_notification:
container_name: "app_new_user_notification"
build:
context: .
dockerfile: ./Account/dev.newusernotification.Dockerfile
links:
- "mongo"
- "rabbitmq"
depends_on:
- "mongo"
- "rabbitmq"
command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "90", "--", "node", “newusernotification.js"]
amqp connection string:
(I tried both ways, with and without a user/pass)
amqp://username:password#rabbitmq:5672
I added the link attribute to the docker-compose file and referenced the name in the .env file(rabbitmq). I tried to run the NewUserNotification.js file from outside the container and it started fine. What could be causing this problem? Connection string issue? Docker-Compose.yml configuration issue? Other?
Seems the environment variable AMQP_RECEIVE_URL is not constructed properly. According to error log the listener is trying to connect to localhost(127.0.0.1) which is not the rabbitmq service container IP. Find the modified configurations for a working sample.
1 docker-compose.yml
version: '2.1'
services:
rabbitmq:
container_name: "4340_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- 15672:15672
- 15671:15671
- 5672:5672
volumes:
- ./rabbitmq/lib:/var/lib/rabbitmq
new_user_notification:
container_name: "app_new_user_notification"
build:
context: .
dockerfile: Dockerfile
env_file:
- ./un.env
links:
- rabbitmq
depends_on:
- rabbitmq
command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "120", "--", "node", "newusernotification.js"]
2 un.env
AMQP_RECEIVE_URL=amqp://guest:guest#rabbitmq:5672
Note that I've passed the AMQP_RECEIVE_URL as an environment variable to new_user_notification service using env_file and got rid of the account service
3 Dockerfile
FROM node:7
WORKDIR /app
COPY newusernotification.js /app
COPY wait-for-it.sh /app
RUN npm install --save seneca
RUN npm install --save seneca-amqp-transport
4 newusernotification.js use the same file in the question.
5 wait-for-it.sh
It is possible that your RabbitMQ service is not fully up, at the time the connection is attempted from the consuming service.
If this is the case, in Docker Compose, you can wait for services to come up using a container called dadarek/wait-for-dependencies.
1). Add a new service waitforrabbit to your docker-compose.yml
waitforrabbit:
image: dadarek/wait-for-dependencies
depends_on:
- rabbitmq
command: rabbitmq:5672
2). Include this service in the depends_on section of the service that requires RabbitMQ to be up.
depends_on:
- waitforrabbit
3). Startup compose
docker-compose run --rm waitforrabbit
docker-compose up -d account new_user_notification
Starting compose in this manner will essentially wait for RabbitMQ to be fully up before the connection from the consuming service is made.

NodeJS: Can't connect to postgreSQL container from another container

I have container with a NodeJS application and it has to be linked to another container with a Postgres db. My docker-compose.yml is:
version: "3"
services:
postgres:
image: "postgres:9.5.6-alpine"
container_name: postgres
volumes:
- /shared_folder/postgresql:/var/lib/postgresql
ports:
- "5432:5432"
web:
build: .
container_name: web
ports:
- "3000:3000"
links:
- postgres
depends_on:
- postgres
command: sh /usr/home/freebsd/wait-for-command.sh -c 'nc -z postgres 5432'
command: sh start.sh
On postgres container there is a db named "bucket", owned by user "user" with password "password".
When I type docker-compose up, and try to link the containers, after this message from console:
[INFO] console - postgres://user:password#localhost:5432/bucket
I get this error:
Error: Connection terminated
at [object Object].<anonymous>
in /node_modules/massive/node_modules/deasync/index.js:46
where is the problem?
You define command twice, which means your wait-for-command.sh is being overridden by sh start.sh and it's probably being run before PostgreSQL is ready to accept connections.

Resources