connecting two docker containers nodejs and mongodb [duplicate] - node.js

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.

Related

Unable to Authenticate Mongo-exporess on Mongo db with Docker Compose

I m trying to connect mongo express and my node app to mongo db with docker compose but I' m unable to authenticate.
Can not connect those service with docker-compose, continusly getting this error:
Could not connect to database using connectionString: mongodb://admin:password#mongo:27017"
The following is my docker-compose.yml
version: '3'
services:
app:
build: .
env_file: .env
restart: always
environment:
- NODE_ENV=production
ports:
- "8080:8080"
volumes:
- .:/usr/src/app
command: yarn dev
depends_on:
- mongodb
networks:
- mongo-compose-network
links:
- mongodb
mongodb:
container_name: mongodb
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
ports:
- "27017:27017"
volumes:
- mongodbdata:/data/db
networks:
- mongo-compose-network
mongo-express:
image: mongo-express
depends_on:
- mongodb
ports:
- "8888:8081"
links:
- mongodb
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_URL=mongodb://admin:password#mongo:27017
networks:
- mongo-compose-network
redis:
image: redis:latest
ports:
- "6379:6379"
networks:
- mongo-compose-network
volumes:
- redisdata:/data
celery:
build: .
command: node celery.js worker --loglevel=info
depends_on:
- app
- mongodb
- redis
networks:
- mongo-compose-network
environment:
- CELERY_BROKER_URL=redis://redis:6379/0
volumes:
mongodbdata:
redisdata:
networks:
mongo-compose-network:
driver: bridge
Resolved by creating an admin user from inside the mongodb container.

Backend can't query dockerized postgresql

I'm running my containers via a docker compose file. They are in the same network and I can ping from my backend container to my database container. I use the database name as the hostname in the connection string and it doesn't bring any errors that it couldn't find the host. Instead, it just hangs up and times out.
I have a test endpoint which is just suppose to test the connection. When you use that endpoint, database container logs "invalid packet length", and on the frontend, nothing happens, then it times out. I have no idea whats wrong. Any help?
version: '3.2'
services:
server:
restart: always
build:
dockerfile: Dockerfile
context: ./nginx
depends_on:
- backend
- frontend
- database
ports:
- '5000:80'
networks:
- app_network
database:
image: postgres:latest
container_name: database
ports:
- "5432:5432"
restart: always
hostname: database
environment:
POSTGRES_PASSWORD: 1234
POSTGRES_USER: postgres
backend:
build:
context: ./backend
dockerfile: ./Dockerfile
image: kalendae:backend
hostname: backend
container_name: backend
environment:
- WAIT_HOSTS=database:5432
- DATABASE_HOST=database
- DATABASE_PORT=5432
- PORT=5051
frontend:
build:
context: ./frontend
dockerfile: ./Dockerfile
image: kalendae:frontend
hostname: frontend
container_name: frontend
environment:
- WAIT_HOSTS=backend:5051
- REACT_APP_BACKEND_HOST=localhost
- REACT_APP_BACKEND_PORT=5051

Make multiple Node js dockerized app communcate to mongodb docker

I've built two NEST app which shares a common database. I've been trying to figure out a way to make these two apps to get data from the Mongo db container. I'm stuck with this process.
docker-compose.yml with app 1
version: '3'
services:
backend:
image: 'example-image_1:1.0.0'
working_dir: /app/example-app-1/backend/example-app-1-api
environment:
- DB_URL=mongodb://0.0.0.0:27017/example_app_1
- BACKEND_PORT=3333
- BACKEND_IP=0.0.0.0
restart: always
network_mode: "host"
ports:
- '3333:3333'
command: ['node', 'main.js']
depends_on:
- mongodb
expose:
- 3333
mongodb:
image: 'mongo:latest'
environment:
- 'MONGODB_DATABASE="example_app_1"'
ports:
- '27017:27017'
expose:
- 27017
docker-compose.yml with app-2
version: '3'
services:
backend:
image: 'example_app_2:1.0.0'
working_dir: /app/example_app_2/backend/example-app-2-api
environment:
- DB_URL=mongodb://0.0.0.0:27017/example_app_2
- BACKEND_PORT=8888
- BACKEND_IP=0.0.0.0
restart: always
ports:
- '8888:8888'
command: ['node', 'main.js']
expose:
- 8888
mongodb:
image: 'mongo:latest'
environment:
- 'MONGODB_DATABASE="example_app_2"'
ports:
- '27017:27017'
expose:
- 27017
I need help in making these app communicate with common container - mongodb
Click link for Architecture Setup
Since you're using 2 different docker-compose.yml you're actually running 2 backend and 2 mongodb on 2 docker-networks
One of the 2 mongo won't start cause the port is already occupied.
Option 1 (nicer):
services:
backend_1:
...
ports:
- '8888:8888'
backend_2:
...
ports:
- '8899:8899'
mongodb:
ports:
- '27017:27017'
This setup provides 3 container on the same network.
Now you can access at mongo from both backends at <mongo_ip>:27017
Option 2 (ugly):
services:
backend:
...
ports:
- '8888:8888'
mongodb:
ports:
- '27017:27017'
And in another docker-compose
services:
backend:
...
ports:
- '8888:8888'
This setup provides 3 container on 2 different network.
In this setup each docker-compose.yml has it's own docker network, so from the second backend service you have to connect to another docker network to access the container.

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.

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