[英]MongooseServerSelectionError ECONNREFUSED with a docker-compose using mongo container
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.我正在尝试在 Docker 中对我的 MERN 堆栈应用程序进行 docker 化,并且我能够使用带有 .yml 文件的 docker-compose 使所有服务在 docker.network 上的容器中运行,但由于某种原因,我的应用程序无法连接到蒙戈服务。 I was able to get the app to work on my localhost but in Docker, it is not working.
我能够让该应用程序在我的本地主机上运行,但在 Docker 中,它无法运行。
Mongo connection with mongoose on backend:后端与 mongoose 的 Mongo 连接:
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: docker-compose.yml文件:
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: 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: Docker终端报错信息:
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.我刚刚开始学习 Docker,所以我主要是在网上查看教程和其他示例。 I have hit a roadblock here though.
不过,我在这里遇到了障碍。
Here is the repo: https://github.com/nickhanrahan/shareport这是回购协议: https://github.com/nickhanrahan/shareport
Can anyone point me in the right direction here?谁能在这里指出我正确的方向? Any help would be appreciated.
任何帮助,将不胜感激。
mongodb uses port 27017 not 27018 mongodb 使用端口 27017 而不是 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
不要忘记
server/db/index.js
的变化
const mongoURI = mongodb://${MONGOUN}:${MONGOPW}@mongodb:27017..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.