Connect MongoDB container with nodejs app - node.js

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.

Related

Connect NodeJS App Running Locally to MongoDB on docker container

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

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

connecting two docker containers nodejs and mongodb [duplicate]

This question already has answers here:
Docker Compose wait for container X before starting Y
(20 answers)
Closed 2 years ago.
I'm trying to connect two conainers using networks docker, but for some reason I'm getting a error:
MongoNetworkError: failed to connect to server [db:27017] on first connect [Error: connect ECONNREFUSED 172.22.0.3:27017
Any suggestion?
Thanks!
docker-compose.yml
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile.dev
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file:
- .env
environment:
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=db
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
ports:
- "3000:3000"
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
networks:
- app-network
command: /home/node/app/node_modules/.bin/nodemon app.js
db:
image: mongo:4.2.9-bionic
container_name: db
restart: unless-stopped
env_file:
- .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
volumes:
- dbdata:/data/db
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
node_modules:
Is it possible that MongoDB inside the container has not yet started when your Node.js app is trying to connect to it?
You should try marking nodejs service as dependent on db service.

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'

Resources