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/groceryListinstead, the same applies, only with theENOTFOUNDflag 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