我正在尝试在Heroku上部署我的Node.js Express MongoDB Dockerized。 构建图像后,在Heroku日志中,我看到:
2020-03-07T12:51:12.880526+00:00 heroku[web.1]: Starting process with command `npm start`
2020-03-07T12:51:16.087665+00:00 app[web.1]:
2020-03-07T12:51:16.087677+00:00 app[web.1]: > esemeny-back@1.0.0 start /usr/src/app
2020-03-07T12:51:16.087678+00:00 app[web.1]: > node server.js
2020-03-07T12:51:16.087678+00:00 app[web.1]:
2020-03-07T12:51:16.937790+00:00 app[web.1]: app error { Error: getaddrinfo ENOTFOUND mongo mongo:27017
2020-03-07T12:51:16.937806+00:00 app[web.1]: at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
2020-03-07T12:51:16.937807+00:00 app[web.1]: name: 'MongoNetworkError',
2020-03-07T12:51:16.937807+00:00 app[web.1]: errorLabels: [ 'TransientTransactionError' ],
2020-03-07T12:51:16.937808+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {} }
2020-03-07T12:51:16.948788+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-03-07T12:51:16.949327+00:00 app[web.1]: npm ERR! errno 1
2020-03-07T12:51:16.951211+00:00 app[web.1]: npm ERR! esemeny-back@1.0.0 start: `node server.js`
2020-03-07T12:51:16.951445+00:00 app[web.1]: npm ERR! Exit status 1
2020-03-07T12:51:16.951838+00:00 app[web.1]: npm ERR!
2020-03-07T12:51:16.952103+00:00 app[web.1]: npm ERR! Failed at the esemeny-back@1.0.0 start script.
2020-03-07T12:51:16.952335+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
使用本地docker-compose,一切正常,我可以从服务器连接到mongo。
这是我的Dockerfile:
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9000
CMD ["npm", "start"]
还有我的docker-compose.yml
version: '3'
services:
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
app:
container_name: docker-node-mongo
restart: always
build: .
ports:
- '9000:3000'
links:
- mongo
最后,这是我来自server.js的猫鼬连接
mongoose
.connect('mongodb://mongo:27017/events_db', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
app.listen(process.env.PORT, err => {
if (!err) {
log.info(`Server started on port ${process.env.PORT}`);
log.info(`Server mode: ${process.env.NODE_ENV}`);
}
});
})
.catch(err => {
log.error(err);
process.exit(1);
});
有什么想法吗?我试图将我的mongo ip添加到Heroku的配置变量中,没有运气。