Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 55 additions & 68 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,50 @@
var raw = require('docker-raw-stream')
var docker = require('docker-remote-api')
var through = require('through2')
var pump = require('pump')
var events = require('events')
var debug = require('debug')('docker-run')

var noop = function() {}

var run = function(image, opts) {
if (!opts) opts = {}

var request = docker(opts.host, {version:'v1.14'})
var that = new events.EventEmitter()
var tty = !!opts.tty
//jshint laxcomma: true, node: true, indent: false, camelcase: false, expr: true, newcap: false, quotmark: false
'use strict';
var raw = require('docker-raw-stream');
var docker = require('docker-remote-api');
var through = require('through2');
var pump = require('pump');
var events = require('events');
var debug = require('debug')('docker-run');
var _ = require('lodash');

var noop = function() {};

var run = function(opts) {

opts = opts || {};

var defaultCreate = {
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
OpenStdin: true,
StdinOnce: true,
Tty: false,
ExposedPorts: {},
Env: [],
Volumes: {}
};

var sopts = {
NetworkMode: opts.net === 'auto' ? (opts.ports ? 'bridge' : 'host') : opts.net,
var defaultStart = {
PortBindings: {},
Binds: [],
Privileged: !!opts.privileged
}
Privileged: false
};

var copts = {
AttachStdin: !opts.fork,
AttachStdout: !opts.fork,
AttachStderr: !opts.fork,
OpenStdin: !opts.fork,
StdinOnce: !opts.fork,
Cmd: opts.argv || [],
Tty: tty,
Image: image,
ExposedPorts: {},
Env: [],
Volumes: {}
}
var sopts = _.defaults(opts.start || {}, defaultStart);
var copts = _.defaults(opts.create || {}, defaultCreate);

if (opts.dns) sopts.Dns = [].concat(opts.dns)
if (opts.entrypoint) copts.Entrypoint = [].concat(opts.entrypoint)

if (opts.ports) {
Object.keys(opts.ports).forEach(function(host) {
var container = opts.ports[host]
if (!/\//.test(container)) container += '/tcp'
copts.ExposedPorts[container] = {}
sopts.PortBindings[container] = [{HostPort:host+''}]
})
}
var request = docker(opts.host, {version:'v1.14'});
var that = new events.EventEmitter();

if (opts.env) {
Object.keys(opts.env).forEach(function(name) {
copts.Env.push(name+'='+opts.env[name])
})
}

if (opts.volumes) {
Object.keys(opts.volumes).forEach(function(host) {
var container = opts.volumes[host]
copts.Volumes[host] = {}
sopts.Binds.push(host+':'+container+':rw')
})
}

that.stdin = opts.fork ? null : through()
that.stderr = opts.fork ? null : through()
that.stdout = opts.fork ? null : through()
that.setMaxListeners(0)
that.stdin = copts.AttachStdin ? through() : null;
that.stderr = copts.AttachStdout ? through() : null;
that.stdout = copts.AttachStderr ? through() : null;
that.setMaxListeners(0);

var ready = function(cb) {
if (that.id) return cb()
Expand All @@ -85,7 +65,7 @@ var run = function(image, opts) {
}

var create = function(cb) {
debug('creating container')
debug('creating container', copts);
var qs = {}
if (opts.name) qs.name = opts.name
request.post('/containers/create', {json: copts, qs:qs}, cb)
Expand All @@ -107,7 +87,7 @@ var run = function(image, opts) {
}
}, function(err, response) {
if (err) return cb(err)
if (tty) return cb(null, stdin, response)
if (copts.Tty) return cb(null, stdin, response)

var parser = response.pipe(raw())
cb(null, stdin, parser.stdout, parser.stderr)
Expand All @@ -122,11 +102,16 @@ var run = function(image, opts) {
}

var remove = function(id, cb) {
if (opts.remove === false) return cb()
debug('removing %s', id)
request.del('/containers/'+id, cb)
request.del('/containers/'+id, {qs:{force:true}}, cb)
}

that.remove = function(cb) {
ready(function() {
remove(that.id, cb)
})
}

var stop = function(id, cb) {
debug('stopping %s', id)
request.post('/containers/'+id+'/stop', {
Expand All @@ -137,7 +122,7 @@ var run = function(image, opts) {
}

var start = function(id, cb) {
debug('starting %s', id)
debug('starting %s', id, sopts)
request.post('/containers/'+id+'/start', {json: sopts}, cb)
}

Expand Down Expand Up @@ -198,10 +183,12 @@ var run = function(image, opts) {

wait(container.Id, function(err, code) {
if (err) return onerror(container.Id, err)
remove(container.Id, function() {
that.emit('exit', code)
that.emit('close')
})
if (opts.remove) {
that.remove(container.Id, function() {
that.emit('exit', code)
that.emit('close')
})
}
})

that.emit('spawn', that.id)
Expand All @@ -213,4 +200,4 @@ var run = function(image, opts) {
return that
}

module.exports = run
module.exports = run
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docker-run",
"version": "2.0.0",
"version": "2.0.1",
"description": "Start a docker image and attach to it",
"main": "index.js",
"dependencies": {
Expand All @@ -9,7 +9,8 @@
"docker-remote-api": "^4.4.0",
"pump": "^1.0.0",
"through2": "^0.6.2",
"minimist": "^1.1.0"
"minimist": "^1.1.0",
"lodash": "^3.9.3"
},
"devDependencies": {
"concat-stream": "^1.4.6",
Expand Down