あいつの日誌β

働きながら旅しています。

CircleCI 上で Docker を立ち上げてみる by Node.js

あらすじ

あるプロジェクトを作成したが開発環境をドキュメント化しないといけないのですが、Ansible などで書いたものをテストする為に Docker を使う事にします。

Enviroments

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.10.5
BuildVersion:   14F1021
% docker --version
Docker version 1.9.0, build 76d6bc9
% VBoxManage --version
5.0.10r104061

ready example package.json

% mkdir practice-node-docker && cd $_

create: package.json

% npm init -f

edit: packegae.json

   "description": "",
   "main": "index.js",
   "scripts": {
+    "start": "node -e 'console.log(\"hello, world!!\")'",
     "test": "echo \"Error: no test specified\" && exit 1"
   },
   "keywords": [],

test run

% npm start

> practice-node-docer@1.0.0 start /Users/ma_okamura/practice-node-docer
> node -e 'console.log("hello, world!!")'

hello, world!!

ready example Dockerfile

% echo 'FROM node:onbuild' > Dockerfile

build

% docker build -t okamuuu/practice-node-docker .

run

% docker run okamuuu/practice-node-docker
npm info it worked if it ends with ok
npm info using npm@3.3.6
npm info using node@v5.0.0
npm info lifecycle practice-node-docer@1.0.0~prestart: practice-node-docer@1.0.0
npm info lifecycle practice-node-docer@1.0.0~start: practice-node-docer@1.0.0

> practice-node-docer@1.0.0 start /usr/src/app
> node -e 'console.log("hello, world!!")'

hello, world!!
npm info lifecycle practice-node-docer@1.0.0~poststart: practice-node-docer@1.0.0
npm info ok 

using circleCI

% npm install --save-dev mocha power-assert
% mkdir test

create: test/test_hello.js

var assert = require('power-assert');
describe('Array', function() {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    }); 
  }); 
});

edit: package.json

   "main": "index.js",
   "scripts": {
     "start": "node -e 'console.log(\"hello, world!!\")'",
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "mocha"
   },

test it

% $(npm bin)/mocha


  Array
    #indexOf()
      ✓ should return -1 when the value is not present


  1 passing (17ms)

create: circle.yml

% cat circle.yml
machine:
 services:
  - docker

dependencies:
 pre:
  - docker info
  - docker build -t okamuuu/practice-node-docker .

test:
 override:
  - docker run okamuuu/practice-node-docker npm run test