Automate Node Server with Docker and Gulp

Automate Node Server with Docker

To automate node server with using docker and gulp. first download the docker image for node server with following command

docker pull sfvishalgupta/imagenode:v0

Running the docker machine create the run.sh file with following code

#!/bin/bash
# setup app server
echo 'Cleaning up previous docker images'
docker rm -f nodeContainer
service apache2 stop

DIR=$( cd `dirname $0`/..; pwd )

docker run -tid \
	--name nodeContainer \
	-p 80:3000 \
	-v ${DIR}/:/var/www/html \
	-v ${DIR}/bin/init.sh:/init.sh \
	-w /var/www/html \
	sfvishalgupta/imagenode:v0 \
    /init.sh

docker ps

with one init.sh file with following command

#!/bin/bash
cd /var/www/html
gulp start

Now run run.sh

this will start the docker machine and bind its 3000 port to your port 80. and map the current directory to the /var/www/html dir which must have the node server code. So just open localhost or 127.0.0.1 and you will be able to see the application.

To enter the docker machine run following command

docker exec -it suzukinode bash

Now create one gulpfile.js in code directory. with following task name start

'use strict';
var gulp = require('gulp'),
nodemon = require('gulp-nodemon');
gulp.task('start', function () {
    nodemon({
        script: 'index.js',
	ext: 'js html',
	env: { 'NODE_ENV': 'development' }
    });
});

Gulp is already in your docker machine. so as soon as we run the docker machine gulp start command with trigger via init.sh file. here nodemon will watch all js and html files if these extentions file got change nodemon will restart the server automatically and you can view changes in browser.

Leave a Reply

Your email address will not be published. Required fields are marked *