Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Apr 30, 2014
1 parent 230a00a commit 4a01c58
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 39 deletions.
51 changes: 33 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Not another Node.js Docker.io Remote API module.
Why `dockerode` is different from other Docker node.js modules:

* **streams** - `dockerode` does NOT break any stream, it passes them to you allowing for some stream voodoo.
* **stream demux** - Supports optional demultiplexing of the new attach stream system implemented in Remote API v1.6.
* **stream demux** - Supports optional demultiplexing of the new attach stream system implemented in Remote API v1.6.
* **entities** - containers and images are defined entities and not random static methods.
* **run** - `dockerode` allow you to seamless run commands in a container ala `docker run`.
* **tests** - `dockerode` really aims to have a good test set, allowing to follow `Docker` changes easily, quickly and painlessly.
Expand All @@ -20,7 +20,7 @@ Why `dockerode` is different from other Docker node.js modules:

## Usage

* Input options are directly passed to Docker.io. Check [Docker Remote API documentation](http://docs.docker.io/en/latest/api/docker_remote_api/) for more details.
* Input options are directly passed to Docker.io. Check [Docker Remote API documentation](http://docs.docker.io/reference/api/docker_remote_api/) for more details.
* Return values are unchanged from Docker, official Docker.io documentation will also apply to them.
* Check the tests for more examples.

Expand Down Expand Up @@ -51,11 +51,17 @@ container.remove(function (err, data) {
//...
```

You may also specify default options for each container's operations, which will always be used for the specified container and operation.

``` js
container.defaultOptions.start.Binds = ["/tmp:/tmp:rw"];
```

### Stopping all containers on a host

``` js
docker.listContainers(function(err, containers) {
containers.forEach(function(containerInfo) {
docker.listContainers(function (err, containers) {
containers.forEach(function (containerInfo) {
docker.getContainer(containerInfo.Id).stop(cb);
});
});
Expand All @@ -64,16 +70,16 @@ docker.listContainers(function(err, containers) {
### Building an Image

``` js
docker.buildImage('archive.tar', {t: imageName}, function(err, response){
docker.buildImage('archive.tar', {t: imageName}, function (err, response){
//...
});
```

### Creating a container:

``` js
docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash']}, function(err, container) {
container.start(function(err, data) {
docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash']}, function (err, container) {
container.start(function (err, data) {
//...
});
});
Expand All @@ -84,18 +90,17 @@ docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash']}, function(err, cont

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

//tty:false
container.attach({stream: true, stdout: true, stderr: true, tty: false}, function(err, stream) {
//http://docs.docker.io/en/latest/api/docker_remote_api_v1.7/#post--containers-(id)-attach
//dockerode may demultiplex the streams for you :)
container.attach({stream: true, stdout: true, stderr: true, tty: false}, function (err, stream) {
//dockerode may demultiplex attach streams for you :)
container.modem.demuxStream(stream, process.stdout, process.stderr);
});

docker.createImage({fromImage: 'ubuntu'}, function(err, stream) {
docker.createImage({fromImage: 'ubuntu'}, function (err, stream) {
stream.pipe(process.stdout);
});

Expand All @@ -107,33 +112,43 @@ docker.createImage({fromImage: 'ubuntu'}, function(err, stream) {
* `image` - container image
* `cmd` - command to be executed
* `stream` - stream(s) which will be used for execution output.
* `[create_options]` - options used for container creation.
* `callback` - callback called when execution ends.

``` js
docker.run('ubuntu', ['bash', '-c', 'uname -a'], process.stdout, function(err, data, container) {
docker.run('ubuntu', ['bash', '-c', 'uname -a'], process.stdout, function (err, data, container) {
console.log(data.StatusCode);
});
```

or, if you want to split stdout and stderr (you must to pass `Tty:false` as an option for this to work)

``` js
docker.run('ubuntu', ['bash', '-c', 'uname -a'], [process.stdout, process.stderr], {Tty:false}, function(err, data, container) {
docker.run('ubuntu', ['bash', '-c', 'uname -a'], [process.stdout, process.stderr], {Tty:false}, function (err, data, container) {
console.log(data.StatusCode);
});
```

Run also returns an EventEmitter supporting the following events: container, stream, data. Allowing stuff like this:

``` js
docker.run('ubuntu', ['bash', '-c', 'uname -a'], [process.stdout, process.stderr], {Tty:false}, function (err, data, container) {
//...
}).on('container', function (container) {
container.defaultOptions.start.Binds = ["/tmp:/tmp:rw"];
});
```

### Equivalent of `docker pull` in `dockerode`:

* `repoTag` - container image name (optionally with tag)
`myrepo/myname:withtag`
* `opts` - extra options passed to create image see [docker api](http://docs.docker.io/en/latest/api/docker_remote_api_v1.8/#create-an-image)
* `opts` - extra options passed to create image.
* `callback` - callback called when execution ends.

``` js
docker.pull('myrepo/myname:tag', function(err, stream) {
// streaming output from pull...
// Also see: http://docs.docker.io/en/latest/api/docker_remote_api_v1.8/#create-an-image
docker.pull('myrepo/myname:tag', function (err, stream) {
// streaming output from pull...
});
```

Expand Down
12 changes: 6 additions & 6 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Docker.prototype.buildImage = function(file, opts, callback) {
callback = opts;
opts = null;
}

var self = this;
var opts = {
path: '/build?',
Expand Down Expand Up @@ -247,19 +247,19 @@ Docker.prototype.run = function(image, cmd, streamo, options, callback) {
callback = options;
options = {};
}

var hub = new EventEmitter();

function handler(err, container) {
if (err) return callback(err, container);

hub.emit('container', container);

container.attach({stream: true, stdout: true, stderr: true}, function handler(err, stream) {
if (err) return callback(err, data);

hub.emit('stream', stream);
hub.emit('stream', stream);

if (streamo) {
if (streamo instanceof Array) {
container.modem.demuxStream(stream, streamo[0], streamo[1]);
Expand Down Expand Up @@ -299,7 +299,7 @@ Docker.prototype.run = function(image, cmd, streamo, options, callback) {
_.extend(optsc, options);

this.createContainer(optsc, handler);

return hub;
};

Expand Down
94 changes: 92 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dockerode",
"description": "Docker.io / Docker remote API implementation.",
"version": "1.2.10",
"version": "1.3.0",
"author": "Pedro Dias <[email protected]>",
"maintainers": [
"apocas <[email protected]>"
Expand Down Expand Up @@ -29,5 +29,95 @@
},
"engines": {
"node": ">= 0.8"
}
},
"contributors": [
{
"name": "Pedro Dias",
"email": "[email protected]",
"url": "https://github.com/apocas",
"contributions": 93
},
{
"name": "James Lal",
"email": "[email protected]",
"url": "https://github.com/lightsofapollo",
"contributions": 12
},
{
"name": "Everton Ribeiro",
"email": "[email protected]",
"url": "https://github.com/nuxlli",
"contributions": 4
},
{
"name": "Sam Rijs",
"email": "[email protected]",
"url": "https://github.com/srijs",
"contributions": 3
},
{
"name": "Mike MacCana",
"email": "[email protected]",
"url": "https://github.com/mikemaccana",
"contributions": 2
},
{
"url": "https://github.com/niclashoyer",
"contributions": 2
},
{
"name": "Alex Wolfe",
"email": "[email protected]",
"url": "https://github.com/alexkwolfe",
"contributions": 1
},
{
"name": "Vincent Woo",
"email": "[email protected]",
"url": "https://github.com/vincentwoo",
"contributions": 1
},
{
"name": "Adam Duncan",
"email": "",
"url": "https://github.com/microadam",
"contributions": 1
},
{
"name": "Geoffrey Bachelet",
"email": "[email protected]",
"url": "https://github.com/ubermuda",
"contributions": 1
},
{
"name": "Josh Matthews",
"email": "[email protected]",
"url": "https://github.com/jmatth",
"contributions": 1
},
{
"name": "Kishore Nallan",
"email": "[email protected]",
"url": "https://github.com/kishorenc",
"contributions": 1
},
{
"name": "Mathias Buus",
"email": "[email protected]",
"url": "https://github.com/mafintosh",
"contributions": 1
},
{
"name": "Shannon Poole",
"email": "[email protected]",
"url": "https://github.com/shannonmpoole",
"contributions": 1
},
{
"name": "Dan Williams",
"email": "[email protected]",
"url": "https://github.com/deedubs",
"contributions": 1
}
]
}
37 changes: 24 additions & 13 deletions test/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("#docker", function() {
this.timeout(120000);

// one image with one tag
var repoTag = 'lightsofapollo/test-taskenv:fail';
var repoTag = 'ubuntu:latest';

// XXX: Should this be an extra abstraction in docker.js?
function locateImage(image, callback) {
Expand Down Expand Up @@ -121,18 +121,29 @@ describe("#docker", function() {

describe("#run", function() {
this.timeout(30000);
it('should report malformed request errors', function(done) {
function handler(err, data) {
expect(err).to.be.ok;
done();

it("should emit partial data", function(done) {
function handler(err, data, container) {
expect(err).to.be.null;
//container is created
expect(container).to.be.ok;

container.remove(function(err, data) {
expect(err).to.be.null;
});
}

docker.run(
'ubuntu',
'exit 1', // this is an invalid parameter type (it should be an array)
process.stdout,
handler
);
var ee = docker.run('ubuntu', ['bash', '-c', 'uname -a'], process.stdout, handler);
ee.on('container', function (container) {
expect(container).to.be.ok;
});
ee.on('stream', function (stream) {
expect(stream).to.be.ok;
});
ee.on('data', function (data) {
expect(data).to.be.ok;
done();
});
});

it("should run a command", function(done) {
Expand Down Expand Up @@ -161,8 +172,8 @@ describe("#docker", function() {

container.inspect(function (err, info) {
expect(err).to.be.null;
expect(info.Name).to.equal('/test')
})
expect(info.Name).to.equal('/test');
});

container.remove(function(err, data) {
expect(err).to.be.null;
Expand Down
Binary file modified test/test.tar
Binary file not shown.

0 comments on commit 4a01c58

Please sign in to comment.