I'm trying to dockerize my MERN stack application in Docker and I'm able to get all the services to run in containers on a docker network using docker-compose with a .yml file but for some reason, my app is not able to connect to the mongo service. I was able to get the app to work on my localhost but in Docker, it is not working.
Mongo connection with mongoose on backend:
const mongoURI = `mongodb://${MONGOUN}:${MONGOPW}@mongodb:27018/names`;
mongoose.connect(mongoURI, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => {
console.log('mongo connection established');
})
.catch((error) => {
console.log(`mongo connect error on ${mongoURI}`);
console.log(error);
});
const db = mongoose.connection;
db
.then(() => {
console.log(`connected to mongoose`);
})
.catch((err) => {
console.log(`error connecting to mongoose`);
console.log(err);
});
docker-compose .yml file:
version: '3.1'
services:
app:
restart: always
build: .
ports:
- 3000:3000
links:
- mongodb
mongodb:
image: mongo
ports:
- 27018:27018
expose:
- "27018"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: names
mongo-express:
image: mongo-express
ports:
- 8080:8081
depends_on:
- mongodb
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_SERVER: mongodb
restart: unless-stopped
Dockerfile:
FROM node:14-alpine
RUN mkdir -p /home/app
COPY . /home/app
WORKDIR "/home/app"
EXPOSE 3000
CMD ["npm", "start"]
Error message in Docker terminal:
app_1 | connected to mongoose
app_1 | mongo connect error on mongodb://admin:password@mongodb:27018/names
app_1 | MongooseServerSelectionError: connect ECONNREFUSED 192.168.48.2:27018
app_1 | at NativeConnection.Connection.openUri (/home/app/node_modules/mongoose/lib/connection.js:846:32)
app_1 | at /home/app/node_modules/mongoose/lib/index.js:351:10
app_1 | at /home/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
app_1 | at new Promise (<anonymous>)
app_1 | at promiseOrCallback (/home/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
app_1 | at Mongoose._promiseOrCallback (/home/app/node_modules/mongoose/lib/index.js:1149:10)
app_1 | at Mongoose.connect (/home/app/node_modules/mongoose/lib/index.js:350:20)
app_1 | at Object.<anonymous> (/home/app/server/db/index.js:7:10)
app_1 | at Module._compile (internal/modules/cjs/loader.js:1085:14)
app_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
app_1 | at Module.load (internal/modules/cjs/loader.js:950:32)
app_1 | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
app_1 | at Module.require (internal/modules/cjs/loader.js:974:19)
app_1 | at require (internal/modules/cjs/helpers.js:93:18)
app_1 | at Object.<anonymous> (/home/app/server/index.js:3:12)
app_1 | at Module._compile (internal/modules/cjs/loader.js:1085:14) {
app_1 | reason: TopologyDescription {
app_1 | type: 'Single',
app_1 | setName: null,
app_1 | maxSetVersion: null,
app_1 | maxElectionId: null,
app_1 | servers: Map(1) { 'mongodb:27018' => [ServerDescription] },
app_1 | stale: false,
app_1 | compatible: true,
app_1 | compatibilityError: null,
app_1 | logicalSessionTimeoutMinutes: null,
app_1 | heartbeatFrequencyMS: 10000,
app_1 | localThresholdMS: 15,
app_1 | commonWireVersion: null
app_1 | }
app_1 | }
I'm just getting started with learning Docker so I've mainly been looking online at tutorials and other examples. I have hit a roadblock here though.
Here is the repo: https://github.com/nickhanrahan/shareport
Can anyone point me in the right direction here? Any help would be appreciated.
CodePudding user response:
mongodb uses port 27017 not 27018
version: '3.1'
services:
app:
restart: always
build: .
ports:
- 3000:3000
links:
- mongodb
mongodb:
image: mongo
ports:
- 27017:27017
expose:
- "27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: names
mongo-express:
image: mongo-express
ports:
- 8080:8081
depends_on:
- mongodb
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
restart: unless-stopped
don't forget changes in server/db/index.js
const mongoURI = mongodb://${MONGOUN}:${MONGOPW}@mongodb:27017..