Node app fails to connect to MongoDB, but only in Docker
up vote
1
down vote
favorite
I have two apps:
- MongoDB (started from the Bitnami MongoDB Docker image)
- My custom node app
The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up
, the backend can not connect to MongoDB.
This is an excerpt of the startup sequence:
mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
[...]
backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'
This is my docker-compose.yml
:
version: '2'
services:
mongodb:
image: bitnami/mongodb:latest
expose:
- 27017
environment:
- ALLOW_EMPTY_PASSWORD=yes
backend:
build: ./backend
environment:
API_HOST: http://localhost:3000/
APP_SERVER_PORT: 3000
expose:
- 3000
volumes:
- ./backend:/app/backend
links:
- mongodb
depends_on:
- mongodb
This is my node call to the DB:
mongoose.connect('mongodb://localhost:27017/groceryList',
useNewUrlParser: true
);
I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:
- It is not that MongoDB is not ready when my node app tries to
connect. I wrapped my connection call into an auto reconnection
function as described here and the error repeats endlessly. It is not just about the "first
connect". - I can publish Port 27017 of the MongoDB container and
happily connect with Robo3T. The DB is definitely working. - When I connect to
mongodb://mongo:27017/groceryList
instead, the same applies, only with theENOTFOUND
flag instead ofECONNREFUSED
.
What am I missing?
- Docker 18.06.1-ce
- docker-compose 1.22.0
- Mongoose 5.3.6
- MongoDB 4.0.3
- Node 11.1.0
- macOS 10.14.1
node.js mongodb docker mongoose docker-compose
add a comment |
up vote
1
down vote
favorite
I have two apps:
- MongoDB (started from the Bitnami MongoDB Docker image)
- My custom node app
The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up
, the backend can not connect to MongoDB.
This is an excerpt of the startup sequence:
mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
[...]
backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'
This is my docker-compose.yml
:
version: '2'
services:
mongodb:
image: bitnami/mongodb:latest
expose:
- 27017
environment:
- ALLOW_EMPTY_PASSWORD=yes
backend:
build: ./backend
environment:
API_HOST: http://localhost:3000/
APP_SERVER_PORT: 3000
expose:
- 3000
volumes:
- ./backend:/app/backend
links:
- mongodb
depends_on:
- mongodb
This is my node call to the DB:
mongoose.connect('mongodb://localhost:27017/groceryList',
useNewUrlParser: true
);
I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:
- It is not that MongoDB is not ready when my node app tries to
connect. I wrapped my connection call into an auto reconnection
function as described here and the error repeats endlessly. It is not just about the "first
connect". - I can publish Port 27017 of the MongoDB container and
happily connect with Robo3T. The DB is definitely working. - When I connect to
mongodb://mongo:27017/groceryList
instead, the same applies, only with theENOTFOUND
flag instead ofECONNREFUSED
.
What am I missing?
- Docker 18.06.1-ce
- docker-compose 1.22.0
- Mongoose 5.3.6
- MongoDB 4.0.3
- Node 11.1.0
- macOS 10.14.1
node.js mongodb docker mongoose docker-compose
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two apps:
- MongoDB (started from the Bitnami MongoDB Docker image)
- My custom node app
The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up
, the backend can not connect to MongoDB.
This is an excerpt of the startup sequence:
mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
[...]
backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'
This is my docker-compose.yml
:
version: '2'
services:
mongodb:
image: bitnami/mongodb:latest
expose:
- 27017
environment:
- ALLOW_EMPTY_PASSWORD=yes
backend:
build: ./backend
environment:
API_HOST: http://localhost:3000/
APP_SERVER_PORT: 3000
expose:
- 3000
volumes:
- ./backend:/app/backend
links:
- mongodb
depends_on:
- mongodb
This is my node call to the DB:
mongoose.connect('mongodb://localhost:27017/groceryList',
useNewUrlParser: true
);
I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:
- It is not that MongoDB is not ready when my node app tries to
connect. I wrapped my connection call into an auto reconnection
function as described here and the error repeats endlessly. It is not just about the "first
connect". - I can publish Port 27017 of the MongoDB container and
happily connect with Robo3T. The DB is definitely working. - When I connect to
mongodb://mongo:27017/groceryList
instead, the same applies, only with theENOTFOUND
flag instead ofECONNREFUSED
.
What am I missing?
- Docker 18.06.1-ce
- docker-compose 1.22.0
- Mongoose 5.3.6
- MongoDB 4.0.3
- Node 11.1.0
- macOS 10.14.1
node.js mongodb docker mongoose docker-compose
I have two apps:
- MongoDB (started from the Bitnami MongoDB Docker image)
- My custom node app
The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up
, the backend can not connect to MongoDB.
This is an excerpt of the startup sequence:
mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
[...]
backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'
This is my docker-compose.yml
:
version: '2'
services:
mongodb:
image: bitnami/mongodb:latest
expose:
- 27017
environment:
- ALLOW_EMPTY_PASSWORD=yes
backend:
build: ./backend
environment:
API_HOST: http://localhost:3000/
APP_SERVER_PORT: 3000
expose:
- 3000
volumes:
- ./backend:/app/backend
links:
- mongodb
depends_on:
- mongodb
This is my node call to the DB:
mongoose.connect('mongodb://localhost:27017/groceryList',
useNewUrlParser: true
);
I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:
- It is not that MongoDB is not ready when my node app tries to
connect. I wrapped my connection call into an auto reconnection
function as described here and the error repeats endlessly. It is not just about the "first
connect". - I can publish Port 27017 of the MongoDB container and
happily connect with Robo3T. The DB is definitely working. - When I connect to
mongodb://mongo:27017/groceryList
instead, the same applies, only with theENOTFOUND
flag instead ofECONNREFUSED
.
What am I missing?
- Docker 18.06.1-ce
- docker-compose 1.22.0
- Mongoose 5.3.6
- MongoDB 4.0.3
- Node 11.1.0
- macOS 10.14.1
node.js mongodb docker mongoose docker-compose
node.js mongodb docker mongoose docker-compose
asked Nov 10 at 23:37
303
264
264
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Your mongodb service is named mongodb
not mongo
.
Try
mongoose.connect('mongodb://mongodb:27017/groceryList',
useNewUrlParser: true
);
The generic form is 'mongodb://mongoServiceName:27017/dbname'
, this uses docker's automatic dns resolution for containers within the same network.
And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your mongodb service is named mongodb
not mongo
.
Try
mongoose.connect('mongodb://mongodb:27017/groceryList',
useNewUrlParser: true
);
The generic form is 'mongodb://mongoServiceName:27017/dbname'
, this uses docker's automatic dns resolution for containers within the same network.
And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.
add a comment |
up vote
1
down vote
accepted
Your mongodb service is named mongodb
not mongo
.
Try
mongoose.connect('mongodb://mongodb:27017/groceryList',
useNewUrlParser: true
);
The generic form is 'mongodb://mongoServiceName:27017/dbname'
, this uses docker's automatic dns resolution for containers within the same network.
And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your mongodb service is named mongodb
not mongo
.
Try
mongoose.connect('mongodb://mongodb:27017/groceryList',
useNewUrlParser: true
);
The generic form is 'mongodb://mongoServiceName:27017/dbname'
, this uses docker's automatic dns resolution for containers within the same network.
And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.
Your mongodb service is named mongodb
not mongo
.
Try
mongoose.connect('mongodb://mongodb:27017/groceryList',
useNewUrlParser: true
);
The generic form is 'mongodb://mongoServiceName:27017/dbname'
, this uses docker's automatic dns resolution for containers within the same network.
And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.
answered Nov 10 at 23:53
Siyu
1,009517
1,009517
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244489%2fnode-app-fails-to-connect-to-mongodb-but-only-in-docker%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown