未加星标

Running Docker from Grunt

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

After a recent post on here, and feedback to it, I'm playing with Node again. And this time, I've tackled my problem of having databases set up for development and verification test runs. As I mentioned before, in my Java world I always have embedded - or psuedo-embedded in some cases - databases that start up as part of the application startup so that the dev runs or the verification test runs will be as self contained as possible. In Node this isn't really very easy to achieve.

Enter Docker.

More specifically, enter Docker Compose , Grunt , and Grunt-Shell . I've managed to cobble together a solution that spawns Docker Compose, then runs the rest of the build, and then at the end of it all shuts down the Docker containers. This means that I can run "grunt start" or "grunt verify", have everything start up, and know that afterwards it's all shut down cleanly.

The important part, of course, is how. Here we go. Note that I'm also using the awesome Task-Master module to help make my Grunt configuration cleaner. This makes no difference to anything except for what you see here.

Firstly, I wrote some Docker Compose files. I've got one for Dev and one for Test. In this case I'm using Neo4J, but you can use pretty much anything at all that has a Docker Image, including your own home-grown ones if you so desire. I've personally created a docker directory in which I've got a dev.yml and a test.yml file. For this cobbled together implementation that doesn't really matter. This is my dev.yml file:

version: '2' services: neo4jdev: image: neo4j:3.1.0 ports: - "7474:7474" - "7687:7687" environment: - NEO4J_AUTH=none

Starting that will give me an environment with Neo4J running, which I can access the Web Admin UI on " http://localhost:7474 " and the Bolt API (from Node) on http://localhost:7687 ".

Next is actually running it. This is done using Grunt-Shell, the config of which is:

const execSync = require('child_process').execSync; const grunt = require('grunt'); function buildDockerCommand(env, cmd) { return `docker-compose -p testing${env} -f docker/${env}.yml ${cmd}`; } module.exports = { 'command': (env) => { process.on('exit', () => { grunt.log.ok('Killing docker'); execSync(buildDockerCommand(env, 'down')); if (env === 'test') { grunt.log.ok('Removing docker containers'); execSync(buildDockerCommand(env, 'rm -f')); } }); return buildDockerCommand(env, 'up -d'); } };

Literally what this does is:

* Sets up a Process Exit hook to stop the containers, and if the environment is "test" to remove them

* Start the containers up

My aliases in Grunt then are:

{ "start": [ "build", "shell:docker:dev:up", "express:dev" ], "verify": [ "build", "build:verify", "shell:docker:test:up", "express:verify", "mochaTest:verify" ] }

Simple as that. The "grunt start" command will now spawn the "dev" Docker environment, and then run my application against it. The "grunt verify" command will spawn the "test" Docker environment, run my application against it, and then run the verification tests against this running application. And after Grunt finishes in both cases - either by the tests finishing or by hitting Ctrl-C - the Docker environment gets shut down.

It does take time to do this, but not a lot. (My verification run is about 10 seconds at present, from start to finish. That doesn't include any UI tests yet though.) The first time you run them on a new machine it will have to download all of the Docker images, but apart from some CI setups that should be a one-off.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: DockerGruntJavaAUTUTAU
分页:12
转载请注明
本文标题:Running Docker from Grunt
本站链接:http://www.codesec.net/view/524418.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(64)