docker > run two nodejs scripts using gulp and nodemon

665 views Asked by At

I'm trying whole day to find some solution how to run two scripts written in nodejs parallel in docker.

I've two files: app/index.js - express app using port 8080 app/rabbit.js - script is connection to rabbitmq only as consumer and processing messages

I'm trying to use gulp and nodemon ( I've found solution on the stackoverflow, but id doesn't work )

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']);

This is always runs second script "app/rabbit.js" twice. I'm open to any solution, but I need to run two nodejs script at once in one docker instance.

Any Ideas? Thanks in advance!

1

There are 1 answers

0
Jindřich Kubát On BEST ANSWER

For anyone who will have same problem, I've found solution.

Step 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"]

Step 2:

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

Step 3:

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

Step 4:

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

Step 5:

Run docker-compose up