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.
Related
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
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.
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'
Hi I'm trying to develop simple api but I couldn't establish connection between api and mongodb. When I send a query to database it hangs and couldn't get response. I am trying to connect to mongo container with this code:
mongoose.connect("mongodb://mongo/micro-linkedin_api_1");
Here is my docker-compose file:
version: "3"
services:
api:
restart: always
build: ./api
ports:
- "3000:3000"
links:
- mongodb
env_file:
- ./api/.env
volumes:
- ./api:/usr/xd/job-bot/api
mongodb:
image: mongo
ports:
- "27017:27017"
scraper:
build: ./scraper
and my folder structure is like this one :
As I can see in docker-compose you defined 2 services:
api and mongodb
in Mongoose docs, this is how you connect:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
You did:
mongoose.connect("mongodb://mongo/micro-linkedin_api_1");
so the host is not correct, you used mongo instead of mongodb as defined in docker-compose services.
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.