gulp-docker-compose
v1.1.3
Published
Gulp <-> Docker Compose binding
Downloads
61
Readme
Gulp Docker Compose
Hey there ;) The binding between gulp
and docker compose
lies ahead.
Installation
Just make sure you have installed docker
, docker-compose
and node
, and the current user is in the docker
group in order to be able to run docker
commands without sudo
.
Usage
Typical gulpfile.js
const gulp = require('gulp');
const clean = require('gulp-clean');
const babel = require('gulp-babel');
const path = require('path');
const plumber = require('gulp-plumber');
const vfs = require('vinyl-fs');
const GulpDockerCompose = require('gulp-docker-compose').GulpDockerCompose;
const srcFolder = `${__dirname}/src/`;
const dstFolder = `${__dirname}/build/`;
// clean the previous build
gulp.task('clean', function() {
return gulp.src(dstFolder, {read: false})
.pipe(clean({force: true}));
});
// compile js
gulp.task('build', ['clean'], function() {
// vfs follows symlinks
return vfs.src(srcFolder+'/**/*.js')
.pipe(plumber())
.pipe(babel({
presets: [
["env", {
"targets": {
"node": "4"
},
"modules": "commonjs",
}],
]
}))
.pipe(vfs.dest(dstFolder));
});
var gulpDocker = new GulpDockerCompose(gulp, {
serviceName: 'app',
tasks: {
run: {
name: 'run',
dependences: ['build'],
},
restart: {
name: 'restart',
dependences: ['build'],
},
watchYML: {
name: 'watch-yml',
},
},
extraArgs: {
upOnRun: '--scale app=3',
upOnYMLChange: '--scale app=3',
},
exposeCLICommands: true,
exposeStdOut: false,
exposeStdErr: true,
projectFolder: __dirname,
});
gulp.task('watch', function() {
gulp.watch([srcFolder+'/**/*'], ['build', 'restart']);
});
gulp.task('default', ['build', 'watch', 'watch-yml', 'run']);
Options and arguments
GulpDockerCompose
takes 2 arguments:
gulp
- the reference togulp
object to work withoptions
- an object of various options described below
Options
serviceName
(optional) - the name of the service to build (typically, the one indocker-compose.yml
which hasbuild
directive)tasks
(optional) - the list of tasks to create, an object with two keys:run
- stands forrun compose
task, andrestart
- for restarthangOnInt
(optional, default:true
) - when theCtrl+C
combo is pressed ongulp watch
, it stopsdocker compose
. If the option is set tofalse
, it will not overrideprocess.on('SIGINT')
handler, but in this case, thedocker compose
(started as a daemon) will not be terminated. You may callgulpDocker.stopDockerCompose()
manually in this case.exposeCLICommands
(optional, default:false
) - being set to true, allows applieddocker-compose cli
commands to be displayedexposeStdOut
(optional, default:false
) - exposestdout
ofcli
commandexposeStdErr
(optional, default:true
) - exposestderr
ofcli
commandprojectFolder
(optional) - sets project current folder. Should be set and correct in order to use 'watchYML' task
Both tasks have the same format: an object with the following keys:
name
- the name of the task (an alias#SERVICE_NAME#
is available and will get replaced with the value ofserviceName
)dependences
- the list of tasks which should be executed beforegulp
enters this task
If no tasks
block specified, they can be created with makeRunTask()
and makeRestartTask()
respectively like the following:
gulpDocker.makeRunTask('run:#SERVICE_NAME#', ['build']);
gulpDocker.makeRestartTask('restart:#SERVICE_NAME#', ['build']);
Typical docker-compose.yml
version: '3'
services:
app:
build: .
depends_on:
- "db"
ports:
- "3101:3012"
environment:
- DB_URL=mongodb://db:27017/mydatabase
- PORT=3012
- ROOT_URL=http://localhost
db:
image: "mongo"
ports:
- "3110:27017"
Note that the corresponding Dockerfile
should be present inside the same folder in order to build the service
.
Under the hood
In fact, the module only launches the following docker-compose
commands:
On run
and restart
tasks:
docker-compose up -d --build
On docker-compose.yml
file changed:
docker-compose up -d --remove-orphans
On Ctrl+C
:
docker-compose stop
License
MIT
Enjoy ;)