I am trying to connect mongodb with my app in docker container. I'm using mongoose package, this is the code that i wrote
mongoose.connect("mongodb://mongo:27016/IssueTracker", { useNewUrlParser: true,useUnifiedTopology: true },(err: Error) => {
err ? console.log(err) : console.log("Mongodb database connected");
});
This i my docker-compose.yml file
version: "3"
services:
app:
container_name: IssueTracker
restart: always
build: .
ports:
- '9000:9000'
links:
- mongo
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27016'
And here is my Dockerfile
FROM node:13
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9000
RUN ["npm","run","dev"]
This the error which i get when i try to run docker-compose up in my docker cmdline
server started on port 9000
MongooseError [MongooseServerSelectionError]: getaddrinfo ENOTFOUND mongo
at new MongooseServerSelectionError (/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:823:32)
at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:333:15)
at Object.<anonymous> (/app/src/app.ts:9:10)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Module.m._compile (/app/node_modules/ts-node/src/index.ts:839:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:842:12)
at Module.load (internal/modules/cjs/loader.js:972:32)
at Function.Module._load (internal/modules/cjs/loader.js:872:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at main (/app/node_modules/ts-node/src/bin.ts:227:14)
at Object.<anonymous> (/app/node_modules/ts-node/src/bin.ts:513:3)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Module.load (internal/modules/cjs/loader.js:972:32) {
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) { 'mongo:27016' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
},
[Symbol(mongoErrorContextSymbol)]: {}
}
Tried to check if any of my container in docker are using the mentioned port so that might create this error and none were using the same port. I'm new to docker so don't know so much about it i tried to check some online solutions but it didn't help
Try creating and using a network instead of link
version: "3"
services:
app:
container_name: IssueTracker
restart: always
build: .
ports:
- '9000:9000'
networks: #here
- my-network
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27016'
networks: #here
- my-network
networks: #here
my-network:
driver: bridge
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With