将NodeJS MongoClient连接到使用Docker的localhost mongodb

macOS Sierra

MongoDB Enterprise> db.version()=> 3.4.7

Node.js 8.9.0

Docker Image Inspect => gist

我有这个代码在Node中运行,然后在一个Docker容器中运行,现在不在Docker容器中运行,下面的容器错误输出。 错误引用了昨天和今天之间触发的拓扑问题。 我不知道那是什么意思。 我能想到的唯一的事情是将nvm从8.2.1切换到8.9.0。 转回没有帮助。 添加重新连接参数也没有帮助。 任何想法将不胜感激。

更新:如果我将uri更改为azure cosmosdb实例,则会运行相同的代码。 所以它只是连接到不想连接的本地mongodb的连接string。 再次,跑了昨天,只是更新。 不知道这里发生了什么….

Hi mongodb://localhost:27017/visafrenchtech MongoDB connecting to mongodb://localhost:27017/visafrenchtech MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:336:35) at emitOne (events.js:116:13) at Pool.emit (events.js:211:7) at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12) at Object.onceWrapper (events.js:317:30) at emitTwo (events.js:126:13) at Connection.emit (events.js:214:7) at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:187:49) at Object.onceWrapper (events.js:315:30) at emitOne (events.js:116:13) 

Node.js输出是

 Hi mongodb://localhost:27017/visafrenchtech MongoDB connecting to mongodb://localhost:27017/visafrenchtech DB mongodb://localhost:27017/visafrenchtech connected { _id: 59f8ab91277b785b1c354f05, url: 'http://visa.lafrenchtech.com/10/find-your-future-employer-in-france-french-tech-visa', logo: 'http://img.gdocker.com/node.js/size_5_1001pharmaciesjpg.jpg', company_url: 'http://www.1001pharmacies.com/', company_name: '1001 Pharmacies', industry: '#HealthTech #Retail', location: 'French Tech Montpellier', number_employees: 28, description: '1001Pharmacies.com is a marketplace dedicated to the online sale of health / beautyproducts. We are intermediaries between Internet users and French pharmacies for online sales.', job_offers: 'Job Offers', job_offers_link: 'http://www.welcometothejungle.co/companies/1001pharmacies', Twitter: '@1001pharma', Facebook: 'https://www.facebook.com/1001Pharmacies/', Instagram: 'https://www.instagram.com/1001pharmacies/?hl=fr', Youtube: 'https://www.youtube.com/channel/UChLwpSs9nHs2xbYdbNEJqQg', address: 'Parc Eureka 256 r Thor, 34000 MONTPELLIER', tranche: '21 - 40' } 

这是DockerFile

 FROM mhart/alpine-node:8 WORKDIR /app COPY package.json ./ RUN npm install COPY . ./ EXPOSE 4000 CMD [ "node", "index.js"]