Connect NodeJS App Running Locally to MongoDB on docker container - node.js

I am getting error when attempting connection from localhost to MongoDB running in docker container
Error: Could not call function connect due to MongoServerSelectionError: //getaddrinfo ENOTFOUND mongo at MongoServerSelectionError: getaddrinfo ENOTFOUND mongo\n //at Timeout._onTimeout
Use Case -
I start my NodeApp locally via the npm start command
The App attempts a connection to MongoDB
I have already started my MongoDB as a container via command
docker-compose -f docker-compose.backing-services.yaml up -d --build mongo mongo02 mongo03
Below is the 'docker-compose.backing-services.yaml' file
services:
mongo:
image: mongo:4.4.3
restart: always
container_name: mongodb
entrypoint: ['/usr/bin/mongod', '--noauth', '--bind_ip_all', '--replSet', 'rs0']
env_file:
- ./.env
ports:
- 27017:27017
volumes:
- mongodata01:/data/db
networks:
- my-network
mongo02:
image: mongo:4.4.3
restart: always
container_name: mongodb02
entrypoint: ['/usr/bin/mongod', '--noauth', '--bind_ip_all', '--replSet', 'rs0']
env_file:
- ./.env
ports:
- 27018:27017
volumes:
- mongodata02:/data/db
networks:
- my-network
mongo03:
image: mongo:4.4.3
restart: always
container_name: mongodb03
entrypoint: ['/usr/bin/mongod', '--noauth', '--bind_ip_all', '--replSet', 'rs0']
env_file:
- ./.env
ports:
- 27019:27017
volumes:
- mongodata03:/data/db
networks:
- my-network
volumes:
mongodata01:
mongodata02:
mongodata03:
networks:
my-network:
name: my-network
Now in my app when I try to connect/ping mongodb
uri = 'mongodb://mongo,mongo02,mongo03:27017'
//Tried different URs too e.g. mongodb://mongo,mongo02,mongo03:27017/?replicaset=rs0 , mongodb://mongo:27017
connectionOptions = { useUnifiedTopology: true }
new MongoClient(uri, connectionOptions).ping
//Throws error - Error: Could not call function connect due to MongoServerSelectionError: //getaddrinfo ENOTFOUND mongo at MongoServerSelectionError: getaddrinfo ENOTFOUND mongo\n //at Timeout._onTimeout
Can someone tell what is wrong or what else is missing?

For mongo URI you must use your mongodb service name instead 127.0.0.1 or localhost. You are missing the environment variable in your docker-compose.backing-services.yaml file, like this:
environment:
- MONGO_URI=mongodb://mongodb-myapp:27017/myapp

Related

Docker compose, accessing a Postgres container from an other container

I have this really simple setup with a web app in one container and a Postgres service running in another container.
I need to connect to the Postgres container and thought PGHOST="db" would point to that container ..?
But I keep getting Error: getaddrinfo ENOTFOUND "db" at GetAddrInfoReqWrap.onlookup that I read as; can't find the "db" host ...
What am I missing here?
version: "3.9"
services:
web:
build: .
ports:
- "8081:3011"
links:
- db
environment:
- PGHOST="db"
- PGDATABASE="testdb"
- PGUSER="postgres"
- PGPASSWORD="postgres"
db:
image: postgres
ports:
- "5432:5432"
volumes:
- /usr/local/var/postgresql#13
Try this config. You don't need quotes when passing env variables. And it is better to use depends_on here to make sure DB is up and running before your app starts.
version: "3.9"
services:
web:
build: .
ports:
- "8081:3011"
depends_on:
- db
environment:
- PGHOST=db
- PGDATABASE=testdb
- PGUSER=postgres
- PGPASSWORD=postgres
db:
image: postgres
ports:
- "5432:5432"
volumes:
- /usr/local/var/postgresql#13

Error: getaddrinfo ENOTFOUND with docker-compose server definition

I'm building a react, Node App and I'm using docker-compose my docker compose definition looks like this:
version: "3"
services:
frontend:
stdin_open: true
container_name: firestore_manager
build:
context: ./client/firestore-app
dockerfile: DockerFile
image: rasilvap/firestore_manager
ports:
- "3000:3000"
volumes:
- ./client/firestore-app:/app
environment:
- BACKEND_HOST=backend
- BACKEND_PORT=8081
depends_on:
- backend
backend:
container_name: firestore_manager_server
build:
context: ./server
dockerfile: Dockerfile
image: rasilvap/firestore_manager_server
ports:
- "8081:8081"
volumes:
- ./server:/app
environment:
- BACKEND_HOST=backend
- BACKEND_PORT=8081
I'm trying to access to the NodeJs backend endpoints using the backend prefix defined in the docker-compose file, but I'm getting an Error: getaddrinfo ENOTFOUND firestore_manager_server, the same is happening using the container name: firestore_manager_server.
As you can see in the next urls:
firestore_manager_server:8081/firestore?collection=test&field=nombre&value=xxxx
backend:8081/firestore?collection=test&field=nombre&value=xxxx
I don't have any problem using localhost.
The next is the result of the docker ps command:
Any ideas?
I believe your url should be
frontend:8081/firestore?collection=test&field=nombre&value=xxxx
instead of
firestore_manager_server:8081/firestore?collection=test&field=nombre&value=xxxx

Backend container can't connect to database - Sequelize, Node, Postgres, Docker

When I try to connect my backend (using Sequelize) I get this following error:
error ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
docker-compose.yml:
version: "3.7"
services:
frontend:
build:
context: ./client
dockerfile: Dockerfile
image: client
ports:
- "3000:3000"
volumes:
- ./client:/usr/src/app
backend:
build:
context: ./server
dockerfile: Dockerfile
image: server
ports:
- "8000:8000"
volumes:
- ./server:/usr/src/app
db:
image: postgres
environment:
POSTGRES_DB: ckl
POSTGRES_USER: postgres
POSTGRES_PASSWORD: docker
ports:
- "5432:5432"
What am I doing wrong ?
Thanks in advance
Assuming your backend is connecting to the db you should add a depends_on:
backend:
build:
context: ./server
dockerfile: Dockerfile
image: server
depends_on:
- db
ports:
- "8000:8000"
volumes:
- ./server:/usr/src/app
The db will now be accessible at the host db:5432 if your application is configured to connect to localhost:5432 or 172.0.0.1:5432 you'll need to replace the hostname localhost with db. Your postgres connection string might also not have a host and might be trying to connect to localhost by default. Should be able to look at sequelize to figure out how to pass a host.

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'

Connect MongoDB container with nodejs app

I have two docker container one with my nodejs webapp and one with a mongo db running.
I build a authentication in my mongo db container and now i get this error message:
mongo | 2018-08-22T13:48:24.102+0000 I ACCESS [conn1] SASL SCRAM-SHA-1 authentication failed for blub on admin from client XXXXX; UserNotFound: Could not find user blub#admin
mongo | 2018-08-22T13:48:24.105+0000 I NETWORK [conn1] end connection XXXXX (0 connections now open)
app | (node:16) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
I build both container with this docker-compose.yml
version: "2"
services:
app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
environment:
- MONGODB_INITDB_ROOT_USERNAME=blub
- MONGODB_INITDB_ROOT_PASSWORD=pass
My app connects with the mongodb like this:
mongoose.connect("mongodb://blub:pass#mongo:27017/");
I found a solution:
My docker-compose:
version: '2'
services:
app:
container_name: app
restart: always
build: .
ports:
- 3000:3000
links:
- mongodb
mongodb:
container_name: mongodb
image: mongo:3.4
ports:
- 27017:27017
volumes:
- ./data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin123456
command: mongod
the connection in the nodejs app: mongoose.connect("mongodb://admin:admin123456#mongodb:27017/");
But i actually canĀ“t figure out, why it isn't working on my first try.

Resources