我的应用程序位于Docker容器中..
FROM keymetrics/pm2-docker-alpine:7
ENV environment development
# Add PM2 modules
RUN pm2 install pm2-mongodb
ADD . .
EXPOSE 3000
CMD ["pm2-docker", "ecosystem.config.js", "--env ${environment}"]
我将PM2与ecosystem.config.js一起使用
module.exports = {
apps: [
{
name: 'api',
script: 'api/index.js',
env: {
PORT: process.env.PORT || 3000,
API_MONGO_URL: process.env.MONGO_URL || 'mongodb://localhost/api',
},
env_production: {
MONGO_URL: `mongodb://${process.env.MONGODB_PORT_27017_TCP_ADDR}:${process.env.MONGODB_PORT_27017_TCP_PORT}/api`,
},
},
],
};
这个应用程序依赖于MongoDB服务......所以我用链接容器运行它..
docker run -it --name api -e environment=production --link mongodb:mongodb api:latest sh
当我在容器中执行此命令时,问题 ...
/app # pm2-docker ecosystem.config.js --env $environment
[STREAMING] Now streaming realtime logs for [all] processes
0|pm2-mong | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
0|pm2-mong | Error: double colon in host identifier
0|pm2-mong | at module.exports (/root/.pm2/node_modules/mongodb/lib/url_parser.js:89:13)
0|pm2-mong | at connect (/root/.pm2/node_modules/mongodb/lib/mongo_client.js:480:16)
0|pm2-mong | at /root/.pm2/node_modules/mongodb/lib/mongo_client.js:234:7
0|pm2-mong | at Function.MongoClient.connect (/root/.pm2/node_modules/mongodb/lib/mongo_client.js:230:12)
0|pm2-mong | at Object.init (/root/.pm2/node_modules/pm2-mongodb/lib/stats/client.js:53:15)
0|pm2-mong | at Object.init (/root/.pm2/node_modules/pm2-mongodb/lib/stats/index.js:78:12)
0|pm2-mong | at /root/.pm2/node_modules/pm2-mongodb/app.js:33:9
0|pm2-mong | at Object.PMX.initModule (/root/.pm2/node_modules/pmx/lib/pmx.js:116:12)
0|pm2-mong | at Object.<anonymous> (/root/.pm2/node_modules/pm2-mongodb/app.js:4:5)
0|pm2-mong | at Module._compile (module.js:571:32)
PM2 | App [pm2-mongodb] with id [0] and pid [21], exited with code [0] via signal [SIGINT]
PM2 | Starting execution sequence in -fork mode- for app name:pm2-mongodb id:0
PM2 | App name:pm2-mongodb id:0 online
导致此错误的原因是我使用pm2-mongodb
模块,我需要将模块配置为以下内容...
pm2 set pm2-mongodb:ip ${MONGODB_PORT_27017_TCP_ADDR}
pm2 set pm2-mongodb:port ${MONGODB_PORT_27017_TCP_PORT}
所以问题是...... 何时/何时我能做到这一点?
MONGODB_PORT_27017_TCP_ADDR
和MONGODB_PORT_27017_TCP_PORT
仅在我run
容器后可用。