dockerode – Not another Docker.io Node.js module

While developing nodechecker.com I looked for Node.js modules that implemented the Docker.io Remote API.

I found a few, the majority was  unfinished lacking a lot of functionality and tests.

Things I was looking for:

  • Complete feature set
  • Tests
  • Leverage streams
  • Good abstraction

I didn’t found a module with these characteristics or with a code base ready to accept contributions.

At the time I ended up forking https://npmjs.org/package/docker.io fixed some bugs it had (this module was revamped recently and it’s better now) and implemented some functionality I needed. But all this was really quick patch, I knew that someday I had to do something about this.

That day came yesterday, and another Docker.io node.js module was born… it’s main objectives are those four points I enumerated in this post.

I called it dockerode (docker+node yes naming stuff isn’t a skill I’m proud of…)

How it works and what it can do

Initialization:

var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var docker2 = new Docker({host: 'http://192.168.1.10', port: 3000});

By leveraging streams it allows to do cool stuff like:

container.attach({stream: true, stdout: true, stderr: true}, function(err, stream) {
  stream.pipe(process.stdout);
});

Containers and images are entities:

docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash']}, function(err, container) {
  container.start(function(err, data) {
    //...
  });
});

var container = docker.getContainer('71501a8ab0f8');
container.inspect(function(err, data) {
  console.log(data);
});

//...

“docker run” implementation:

//docker.run(image, cmd, stream, temporary, callback)
docker.run('ubuntu', 'uname -a', process.stdout, true, function(err, data) {
  console.log(data.StatusCode);
});

Installation and tests

npm install dockerode

Tests use mocha and chai.js

Status

  • Feature set is almost complete, tests are about 85%.
  • New WebSockets endpoints added in Docker v0.6 will be supported soon.
Advertisements