在Docker中使用pm2启动实现Nodejs

时间:2018-05-18 05:32:38

标签: node.js docker pm2

我有一个nodejs个应用。该应用程序以pm2运行。当我执行" pm2启动app.js"。 pm2开始了。但当我卷曲http://localhost:3000时,它会说:Failed to connect to localhost port 3000: Connection refused。但是3000港口是开放的。我很困惑,请帮帮我。

我的Dockerfile

FROM keymetrics/pm2:latest-alpine
# Create app directory
RUN mkdir -p /usr/cl/app
WORKDIR /usr/cl/app
# Install app dependencies
COPY package*.json ./
COPY  . .
RUN npm install
RUN npm install pm2 -g
EXPOSE 3000
CMD [ "npm", "start" ]

我的Package.json

{
  "name": "cs",
   "version": "1.0.0",
   "description": "Cs",
   "main": "app.js",
   "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "pm2 start app.js"
  },
   "author": "",
   "license": "ISC",
   "dependencies": {
    "async": "^2.6.0",
    "axios": "^0.18.0",
    "base64-encode-decode": "0.0.3",
    "body-parser": "^1.18.3",
    "cors": "^2.8.4",
    "csv": "^1.2.1",
    "csv2sql-lite": "0.0.6",
    "dotenv": "^4.0.0",
    "epipebomb": "^1.0.0",
    "express": "^4.16.3",
    "fast-csv": "^2.4.1",
    "getmac": "^1.4.1",
    "ini": "^1.3.5",
    "js-md5": "^0.7.3",
    "multer": "^1.3.0",
    "node-cache": "^4.2.0",
    "node-xlsx": "^0.12.0",
    "nodemailer": "^4.6.4",
    "os": "^0.1.1",
    "pg": "^6.4.2",
    "serve-static": "^1.13.2",
    "trim": "0.0.1"
   } 
}

创建了Docker镜像和容器。但是当我卷曲时http://localhost:3000 错误说"无法连接到localhost端口3000:连接被拒绝。但是3000港口是开放的。我很困惑,请帮帮我。

app.js文件是

"var express =  require('express');
 var bodyparser = require('body-parser');
 var cors = require('cors');
 var path = require('path');
 require('dotenv').config({path: 'inireader.env'});
 var app =  express();
 const port = 3000;
 const routepostgres = require('./routes/routepostgres');

 app.use(cors());
 app.use(bodyparser.urlencoded({ extended: false }));
  app.use(bodyparser.json());
   app.use(express.static(path.join(__dirname,'public')))
    app.use('/api', routepostgres);
    app.listen(port, function(req, res){
      console.log('Server started at port:'+ port);
        });
       app.get('/', function(req, res){
       res.send('Welcome @ browser');
       console.log('Welcome @ Console');
     });

0 个答案:

没有答案