docker>使用gulp和nodemon运行两个nodejs脚本

时间:2017-01-06 16:49:16

标签: node.js docker gulp nodemon

我整整一天都想找到一些解决方法,如何在docker中并行运行两个以nodejs编写的脚本。

我有两个档案: app / index.js - 使用端口8080表达app app / rabbit.js - 脚本仅作为消费者和处理消息连接到rabbitmq

我试图使用gulp和nodemon(我已经在stackoverflow上找到了解决方案,但是id不起作用)

var gulp = require('gulp')
var gulputil = require('gulp-util');
var child_process = require('child_process');
var nodemon = require('gulp-nodemon');

var processes = {server1: null, server2: null};

gulp.task('start:server', function (cb) {

processes.server1 = nodemon({
    script: "app/index.js",
    ext: "js"
});

processes.server2 = nodemon({
    script: "app/rabbit.js",
    ext: "js"
});

cb(); // For parallel execution accept a callback.
      // For further info see "Async task support" section here:
      // https://github.com/gulpjs/gulp/blob/master/docs/API.md
});

process.on('exit', function () {
 // In case the gulp process is closed (e.g. by pressing [CTRL + C]) stop    both processes
processes.server1.kill();
processes.server2.kill();
});

gulp.task('run', ['start:server']);
gulp.task('default', ['run']);

这总是运行第二个脚本" app / rabbit.js"两次。我打开任何解决方案,但我需要在一个docker实例中一次运行两个nodejs脚本。

任何想法? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

对于任何有同样问题的人,我找到了解决方案。

第1步:

Create two docker files Dockerfile-api, Dockerfile-messages
As command RUN in the dockerfile use
  a. CMD ["npm", "run", "start-api"]
  b. CMD ["npm", "run", "start-messages"]

第2步:

In the package.json add lines:
 "scripts": {     
   "start-api": "gulp --gulpfile app/gulpfile-api.js",
   "start-messages": "gulp --gulpfile app/gulpfile-messages.js"
  }

第3步:

Obviously create two gulp files, each gulp file will have his own script.

第4步:

Create two services in docker-compose.yml file each witch different DockerFile

第5步:

Run docker-compose up