I've created a node.js
application and it was able to connect to docker (mongodb and mongo-express) containers. But when i create an image and try to run that container it is failing to connect to mongodb. Here is my Dockerfile
FROM node
ENV MONGO_DB_USERNAME=admin
ENV MONGO_DB_PWD=password
RUN mkdir -p /home/app
COPY . /home/app
CMD [ "node", "/home/app/dist/server.js" ]
If i create an image based on this docker file it works but when i run the image with the run
command.
docker run myapp
Here is the output that i'm getting.
The server is running on port: 3001
/home/app/node_modules/mongoose/lib/helpers/promiseOrCal
throw error;
^
MongooseServerSelectionError: connect ECONNREFUSED 127.0
at Connection.openUri (/home/app/node_modules/mongoo
at /home/app/node_modules/mongoose/lib/index.js:344:
at promiseOrCallback (/home/app/node_modules/mongoos
at Mongoose._promiseOrCallback (/home/app/node_modul
at Mongoose.connect (/home/app/node_modules/mongoose
at Object.<anonymous> (/home/app/dist/routes/index.j
at Module._compile (node:internal/modules/cjs/loader
at Module._extensions..js (node:internal/modules/cjs
at Module.load (node:internal/modules/cjs/loader:998
at Module._load (node:internal/modules/cjs/loader:83
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host:
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 57129160,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED 1
at connectionFailureError (/home/app/node_mo20)
at Socket.<anonymous> (/home/app/node_module
at Object.onceWrapper (node:events:628:26)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destro
at emitErrorCloseNT (node:internal/streams/d
at process.processTicksAndRejections (node:i
[Symbol(errorLabels)]: Set(0) {}
}
}
},
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
Node.js v18.7.0
But if i check the running containers i see that mongo-express and mongodb are actually running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
2196e961a20f mongo-express "tini -- /docker-ent…" 58 minutes ago Up 57 minutes 0.0.0.0:8081->8081/tcp
app-mongo-express-1
901ea18d6f4b mongo "docker-entrypoint.s…" 58 minutes ago Up 58 minutes 0.0.0.0:27017->27017/tcp
app-mongodb-1
What may be posibly the problem?