我尝试通过docker-compose将一个Node.JS容器与一个MongoDB容器连接起来,但是系统性地,node.js返回了ECONNREFUSED错误。
错误信息:
NodeJS代码
Dockerfile(./api)
如何解决这个错误?
错误信息:
nodejs_1 | /code/node_modules/mongoose/node_modules/mongodb/lib/server.js:228
nodejs_1 | process.nextTick(function() { throw err; })
nodejs_1 |
nodejs_1 | Error: connect ECONNREFUSED
nodejs_1 | at exports._errnoException (util.js:746:11)
nodejs_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
NodeJS代码
var db = 'mongodb://database:27017/wondrapi';
mongoose.connect(db);
docker-compose.yml
web:
build: ./web
ports:
- "8080:80"
links:
- nodejs
volumes:
- ./web:/usr/share/nginx/html:ro
nodejs:
build: ./api
ports:
- "8081:3000"
links:
- database
command: npm start
database:
image: mongo
volumes:
- db:/data/db
ports:
- 27017
Dockerfile(./api)
FROM node
ADD package.json /code/
WORKDIR /code
RUN npm install
ADD . /code
如何解决这个错误?
entrypoint: ./wait-for-it.sh mongo:27017 -- npm start
。 - antimatter