Skip to content

Commit 4c0c45e

Browse files
committed
exec identity, v2.0.4
1 parent 069a580 commit 4c0c45e

File tree

6 files changed

+207
-131
lines changed

6 files changed

+207
-131
lines changed

examples/exec_running_container.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
var Docker = require('../lib/docker');
22

3-
var docker = new Docker({socketPath: '/var/run/docker.sock'});
3+
var docker = new Docker({
4+
socketPath: '/var/run/docker.sock'
5+
});
46

57
/**
68
* Get env list from running container
79
* @param container
810
*/
911
function runExec(container) {
10-
options = {
11-
"AttachStdout": true,
12-
"AttachStderr": true,
13-
"Tty": false,
14-
Cmd: ["env"]
15-
};
16-
container.exec(options, function (err, data) {
17-
container.execstart(data.Id, function (err, stream) {
18-
if (err != null) {
19-
callback(err, null);
20-
return;
21-
}
22-
23-
stream.setEncoding('utf8');
24-
stream.on('data', function (chunk) {
25-
console.log(chunk);
26-
});
27-
stream.on('end', function () {
28-
console.log('--Done--');
29-
});
30-
});
31-
})
32-
}
12+
options = {
13+
"AttachStdout": true,
14+
"AttachStderr": true,
15+
"Tty": false,
16+
Cmd: ["env"]
17+
};
18+
container.exec(options, function(err, exec) {
19+
if (err) return;
3320

34-
docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/ls', '/stuff'], "Volumes": {"/stuff": {}}}, function (err, container) {
35-
container.attach({stream: true, stdout: true, stderr: true, tty: true}, function (err, stream) {
36-
stream.pipe(process.stdout);
21+
exec.start(function(err, stream) {
22+
if (err) return;
3723

38-
container.start({"Binds": ["/home/vagrant:/stuff"]}, function (err, data) {
39-
runExec(container);
40-
});
24+
stream.setEncoding('utf8');
25+
stream.pipe(process.stdout);
4126
});
27+
});
28+
}
29+
30+
docker.createContainer({
31+
Image: 'ubuntu',
32+
Cmd: ['/bin/bash']
33+
}, function(err, container) {
34+
container.start({}, function(err, data) {
35+
runExec(container);
36+
});
4237
});

examples/external_volume.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
var Docker = require('dockerode');
1+
var Docker = require('../lib/docker');
22

3-
var docker = new Docker({socketPath: '/var/run/docker.sock'});
3+
var docker = new Docker({
4+
socketPath: '/var/run/docker.sock'
5+
});
46

5-
docker.createContainer({ Image: 'ubuntu', Cmd: ['/bin/ls','/stuff'], "Volumes":{"/stuff": {}} }, function (err, container) {
6-
container.attach({stream: true, stdout: true, stderr: true, tty: true}, function (err, stream) {
7+
docker.createContainer({
8+
Image: 'ubuntu',
9+
Cmd: ['/bin/ls', '/stuff'],
10+
"Volumes": {
11+
"/stuff": {}
12+
}
13+
}, function(err, container) {
14+
container.attach({
15+
stream: true,
16+
stdout: true,
17+
stderr: true,
18+
tty: true
19+
}, function(err, stream) {
720
stream.pipe(process.stdout);
821

9-
container.start({"Binds":["/home/vagrant:/stuff"]}, function (err, data) {
22+
container.start({
23+
"Binds": ["/home/vagrant:/stuff"]
24+
}, function(err, data) {
1025
console.log(data);
1126
});
1227
});

lib/container.js

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var _ = require('underscore');
1+
var _ = require('underscore'),
2+
Exec = require('./exec');
23

34
/**
45
* Represents a Container
@@ -10,17 +11,17 @@ var Container = function(modem, id) {
1011
this.id = id;
1112

1213
this.defaultOptions = {
13-
top: {},
14-
start: {},
15-
commit: {},
16-
stop: {},
14+
top: {},
15+
start: {},
16+
commit: {},
17+
stop: {},
1718
restart: {},
18-
resize: {},
19-
attach: {},
20-
remove: {},
21-
copy: {},
22-
kill: {},
23-
exec: {}
19+
resize: {},
20+
attach: {},
21+
remove: {},
22+
copy: {},
23+
kill: {},
24+
exec: {}
2425
};
2526
};
2627

@@ -45,7 +46,9 @@ Container.prototype.inspect = function(callback) {
4546
callback(err, data);
4647
});
4748
} else {
48-
return JSON.stringify({id: this.id});
49+
return JSON.stringify({
50+
id: this.id
51+
});
4952
}
5053
};
5154

@@ -147,7 +150,6 @@ Container.prototype.start = function(opts, callback) {
147150

148151
/**
149152
* Pause
150-
* Warning: Uses an undocumented Docker endpoint. :)
151153
* @param {Object} opts Pause options (optional)
152154
* @param {Function} callback Callback
153155
*/
@@ -174,7 +176,6 @@ Container.prototype.pause = function(opts, callback) {
174176

175177
/**
176178
* Unpause
177-
* Warning: Uses an undocumented Docker endpoint. :)
178179
* @param {Object} opts Unpause options (optional)
179180
* @param {Function} callback Callback
180181
*/
@@ -206,53 +207,28 @@ Container.prototype.unpause = function(opts, callback) {
206207
* @param {function} callback
207208
*/
208209
Container.prototype.exec = function(opts, callback) {
209-
if (!callback && typeof(opts) === 'function') {
210-
callback = opts;
211-
opts = null;
212-
}
210+
var self = this;
213211

214-
var optsf = {
215-
path: '/containers/' + this.id + '/exec',
216-
method: 'POST',
217-
statusCodes: {
218-
201: true,
219-
404: "no such container",
220-
500: "server error"
221-
},
222-
options: _.extend({}, this.defaultOptions.exec, opts)
223-
};
224-
225-
this.modem.dial(optsf, function(err, data) {
226-
callback(err, data);
227-
});
228-
};
212+
if (!callback && typeof(opts) === 'function') {
213+
callback = opts;
214+
opts = {};
215+
}
229216

230-
/**
231-
* Run the exec call that was setup.
232-
*
233-
* @param {object} opts should be passed from exec call return {Id:xxxxxx}
234-
* @param {function} callback
235-
*/
236-
Container.prototype.execstart = function(id, opts, callback) {
237-
if (!callback && typeof(opts) === 'function') {
238-
callback = opts;
239-
opts = null;
240-
}
241-
var optsf = {
242-
path: '/exec/' + id + '/start',
243-
method: 'POST',
244-
isStream: true,
245-
statusCodes: {
246-
200: true,
247-
404: "no such container",
248-
500: "Container not running"
249-
},
250-
options: _.extend({}, this.defaultOptions.exec, opts)
251-
};
217+
var optsf = {
218+
path: '/containers/' + this.id + '/exec',
219+
method: 'POST',
220+
statusCodes: {
221+
201: true,
222+
404: "no such container",
223+
500: "server error"
224+
},
225+
options: _.extend({}, this.defaultOptions.exec, opts)
226+
};
252227

253-
this.modem.dial(optsf, function(err, data) {
254-
callback(err, data);
255-
});
228+
this.modem.dial(optsf, function(err, data) {
229+
if(err) return callback(err, data);
230+
callback(err, new Exec(self.modem, data.Id));
231+
});
256232
};
257233

258234
/**

lib/exec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var _ = require('underscore');
2+
3+
/**
4+
* Represents an Exec
5+
* @param {Object} modem docker-modem
6+
* @param {String} id Exec's ID
7+
*/
8+
var Exec = function(modem, id) {
9+
this.modem = modem;
10+
this.id = id;
11+
};
12+
13+
/**
14+
* Start the exec call that was setup.
15+
*
16+
* @param {object} options
17+
* @param {function} callback
18+
*/
19+
Exec.prototype.start = function(opts, callback) {
20+
if (!callback && typeof(opts) === 'function') {
21+
callback = opts;
22+
opts = {};
23+
}
24+
25+
var optsf = {
26+
path: '/exec/' + this.id + '/start',
27+
method: 'POST',
28+
isStream: true,
29+
statusCodes: {
30+
200: true,
31+
404: "no such exec",
32+
500: "container not running"
33+
},
34+
options: opts
35+
};
36+
37+
this.modem.dial(optsf, function(err, data) {
38+
callback(err, data);
39+
});
40+
};
41+
42+
/**
43+
* Resize the exec call that was setup.
44+
*
45+
* @param {object} options
46+
* @param {function} callback
47+
*/
48+
Exec.prototype.resize = function(opts, callback) {
49+
if (!callback && typeof(opts) === 'function') {
50+
callback = opts;
51+
opts = null;
52+
}
53+
var optsf = {
54+
path: '/exec/' + this.id + '/resize?',
55+
method: 'POST',
56+
statusCodes: {
57+
200: true,
58+
404: "no such exec",
59+
500: "container not running"
60+
},
61+
options: opts
62+
};
63+
64+
this.modem.dial(optsf, function(err, data) {
65+
callback(err, data);
66+
});
67+
};
68+
69+
70+
module.exports = Exec;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "dockerode",
33
"description": "Docker.io / Docker remote API implementation.",
4-
"version": "2.0.3",
4+
"version": "2.0.4",
55
"author": "Pedro Dias <petermdias@gmail.com>",
66
"maintainers": [
77
"apocas <petermdias@gmail.com>"

0 commit comments

Comments
 (0)