Docker node app is not connecting the mongo db

HI Team,

I have created mongo by given the below command

docker run --rm -d -p 27017:27017 --name mongodb mongo

I have build image node app from given below docker file

FROM node:alpine


COPY package.json .

RUN npm install

COPY . .


CMD [ "npm", "run", "start" ]

du the container from given below command

docker run --name todo-app -p 8000:8000 todoapp

But still i am not able to connect my docker to node

it is saying

Unable to connect to MongoDB
MongooseServerSelectionError: connect ECONNREFUSED
    at Connection.openUri (/app/node_modules/mongoose/lib/connection.js:825:32)
    at /app/node_modules/mongoose/lib/index.js:414:10
    at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:11:14)
    at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1288:10)
    at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:413:20)
    at Object.<anonymous> (/app/src/index.js:48:10)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  code: undefined

Yes, containers are for isolation. You can’t just access one container from another, without setting it up explicitly. In your case it does not work because is localhost inside the container, not the localhost of your host, on which you opened port 27017.

To make it work, create a Docker network and attach both containers to it. Then you can use hostname mongodb in the node container to connect to it. The mongodb container does not need to expose 27017 externally. thats even better for security.

Alternatively you can leave it as it is and just use the regular IP of your host (not the, but the other one) to connect from inside the node container to the host and the exposed port of mongodb.

@bluepuma77’s solution is the right one, but there is an alternative if that mongodb is used only by that single nodejs app and you want to keep using the loopback IP address. Start the nodejs app container this way:

docker run --name todo-app -p 8000:8000 --network container:mongo todoapp

This way the nodejs container will get the same loopback interface, meaning they will use the same network namespace. It would work, but if an admin user has more rights from localhost, that would make the app less secure as the nodejs app could use that user.

1 Like

My issue is resolved after i have added the network object which i have created.

Before that, i was using the IP of MongoDB by inspecting the container of MongoDB. that also worked.

Thanks for the solution.