【问题标题】:docker > run two nodejs scripts using gulp and nodemondocker > 使用 gulp 和 nodemon 运行两个 nodejs 脚本
【发布时间】:2017-05-21 11:45:57
【问题描述】:

我整天都在努力寻找一些解决方案,如何在 docker 中并行运行两个用 nodejs 编写的脚本。

我有两个文件: app/index.js - 使用端口 8080 表达应用程序 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 脚本。

有什么想法吗? 提前致谢!

【问题讨论】:

    标签: node.js docker gulp nodemon


    【解决方案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 
    

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 2017-11-22
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2019-05-08
      相关资源
      最近更新 更多