Mongo连接拒绝了ECCONREFUSED

我有一个应用程序在Docker容器中运行。 在日志中容器启动数据库,并让它监听端口27017

在后来的应用程序,build立了其余的网站,它试图连接到该端口上的数据库,并失败ECONNREFUSED 127.0.0.1:27017

我想这可能是一个很基本的东西,我不理解这里,但帮助将不胜感激

Starting maroon_db_1 Recreating maroon_web_1 Attaching to maroon_db_1, maroon_web_1 db_1 | 2016-08-22T10:50:27.114+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=e3a671f93ded db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] db version v3.2.9 db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013 db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] allocator: tcmalloc db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] modules: none db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] build environment: db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] distmod: debian71 db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] distarch: x86_64 db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] target_arch: x86_64 db_1 | 2016-08-22T10:50:27.115+0000 I CONTROL [initandlisten] options: {} db_1 | 2016-08-22T10:50:27.150+0000 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. db_1 | 2016-08-22T10:50:27.150+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' db_1 | 2016-08-22T10:50:27.508+0000 I CONTROL [initandlisten] db_1 | 2016-08-22T10:50:27.522+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data' db_1 | 2016-08-22T10:50:27.522+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker db_1 | 2016-08-22T10:50:27.543+0000 I NETWORK [initandlisten] waiting for connections on port 27017 web_1 | web_1 | > mern-starter@2.0.0 bs /usr/src/app web_1 | > npm run clean && npm run build && npm run build:server && npm run start:prod web_1 | web_1 | web_1 | > mern-starter@2.0.0 clean /usr/src/app web_1 | > rimraf dist web_1 | web_1 | web_1 | > mern-starter@2.0.0 build /usr/src/app web_1 | > cross-env NODE_ENV=production webpack --config webpack.config.prod.js web_1 | web_1 | This preset is no longer necessary and can be replaced by the following: web_1 | presets: [ web_1 | ["es2015", { "modules": false }] web_1 | ] web_1 | Hash: afd840974c1321403cd5 web_1 | Version: webpack 2.1.0-beta.8 web_1 | Time: 19804ms web_1 | Asset Size Chunks Chunk Names web_1 | app.edcea8de66cc864a67a5.js 375 kB 3 [emitted] app web_1 | bbaeb5f32b7042f0def39648a1d111b9.png 15.8 kB [emitted] web_1 | vendor.js 148 kB 0 [emitted] vendor web_1 | 1.b6dee935292b81563830.js 7.85 kB 1 [emitted] web_1 | 2.6f2172bc1184d6c25200.js 1.55 kB 2 [emitted] web_1 | chunk-manifest.json 97 bytes [emitted] web_1 | app.edcea8de66cc864a67a5.css 3.04 kB 3 [emitted] app web_1 | app.b6dee935292b81563830.css 708 bytes 1 [emitted] web_1 | app.b6dee935292b81563830.css.map 105 bytes 1 [emitted] web_1 | app.edcea8de66cc864a67a5.css.map 105 bytes 3 [emitted] app web_1 | manifest.json 508 bytes [emitted] web_1 | [648] multi app 28 bytes {3} [built] web_1 | [649] multi vendor 40 bytes {0} [built] web_1 | + 655 hidden modules web_1 | Child extract-text-webpack-plugin: web_1 | + 2 hidden modules web_1 | Child extract-text-webpack-plugin: web_1 | + 2 hidden modules web_1 | Child extract-text-webpack-plugin: web_1 | + 2 hidden modules web_1 | Child extract-text-webpack-plugin: web_1 | + 2 hidden modules web_1 | Child extract-text-webpack-plugin: web_1 | + 2 hidden modules web_1 | Child extract-text-webpack-plugin: web_1 | + 3 hidden modules web_1 | web_1 | > mern-starter@2.0.0 build:server /usr/src/app web_1 | > cross-env NODE_ENV=production webpack --config webpack.config.server.js web_1 | web_1 | This preset is no longer necessary and can be replaced by the following: web_1 | presets: [ web_1 | ["es2015", { "modules": false }] web_1 | ] web_1 | babel-plugin-webpack-loader: web_1 | To avoid caching errors you need to set BABEL_DISABLE_CACHE=1 environment variable. web_1 | More information at issue #36 web_1 | Hash: eaa3e07d940766fdd179 web_1 | Version: webpack 2.1.0-beta.8 web_1 | Time: 74596ms web_1 | Asset Size Chunks Chunk Names web_1 | server.bundle.js 75.5 kB 0 [emitted] main web_1 | + 63 hidden modules web_1 | web_1 | > mern-starter@2.0.0 start:prod /usr/src/app web_1 | > cross-env NODE_ENV=production node index.js web_1 | web_1 | was local web_1 | server config { mongoURL: 'mongodb://localhost:27017/mern-starter', web_1 | port: 3030 } web_1 | MERN is running on port: 3030! Build something amazing! web_1 | Please make sure Mongodb is installed and running! web_1 | web_1 | /usr/src/app/node_modules/mongodb/lib/server.js:242 web_1 | process.nextTick(function() { throw err; }) web_1 | ^ web_1 | Error: connect ECONNREFUSED 127.0.0.1:27017 web_1 | at Object.exports._errnoException (util.js:837:11) web_1 | at exports._exceptionWithHostPort (util.js:860:20) web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14) web_1 | web_1 | npm ERR! Linux 4.4.15-moby web_1 | npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "start:prod" web_1 | npm ERR! node v4.0.0 web_1 | npm ERR! npm v2.14.2 web_1 | npm ERR! code ELIFECYCLE web_1 | npm ERR! mern-starter@2.0.0 start:prod: `cross-env NODE_ENV=production node index.js` web_1 | npm ERR! Exit status 1 web_1 | npm ERR! web_1 | npm ERR! Failed at the mern-starter@2.0.0 start:prod script 'cross-env NODE_ENV=production node index.js'. web_1 | npm ERR! This is most likely a problem with the mern-starter package, web_1 | npm ERR! not with npm itself. web_1 | npm ERR! Tell the author that this fails on your system: web_1 | npm ERR! cross-env NODE_ENV=production node index.js web_1 | npm ERR! You can get their info via: web_1 | npm ERR! npm owner ls mern-starter web_1 | npm ERR! There is likely additional logging output above. web_1 | web_1 | npm ERR! Please include the following file with any support request: web_1 | npm ERR! /usr/src/app/npm-debug.log web_1 | web_1 | npm ERR! Linux 4.4.15-moby web_1 | npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "bs" web_1 | npm ERR! node v4.0.0 web_1 | npm ERR! npm v2.14.2 web_1 | npm ERR! code ELIFECYCLE web_1 | npm ERR! mern-starter@2.0.0 bs: `npm run clean && npm run build && npm run build:server && npm run start:prod` web_1 | npm ERR! Exit status 1 web_1 | npm ERR! web_1 | npm ERR! Failed at the mern-starter@2.0.0 bs script 'npm run clean && npm run build && npm run build:server && npm run start:prod'. web_1 | npm ERR! This is most likely a problem with the mern-starter package, web_1 | npm ERR! not with npm itself. web_1 | npm ERR! Tell the author that this fails on your system: web_1 | npm ERR! npm run clean && npm run build && npm run build:server && npm run start:prod web_1 | npm ERR! You can get their info via: web_1 | npm ERR! npm owner ls mern-starter web_1 | npm ERR! There is likely additional logging output above. web_1 | web_1 | npm ERR! Please include the following file with any support request: web_1 | npm ERR! /usr/src/app/npm-debug.log maroon_web_1 exited with code 1 

你在docker-compose.yml中暴露了MongoDB的port 27017吗? 但是,如果您不想将MongoDB公开到互联网(推荐的方式),您可以使用Docker的内部DNS来连接MongoDB(docker-compose.yml的示例):

mongodb: image: mongo:3.2 web: build: . environment: - MONGODB_URL=mongodb://mongodb:27017/mern-starter

然后在您的应用程序中,您可以使用MONGODB_URL环境variables来连接MongoDB。