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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function push (imageName, auth, onProgress) {
module.exports = {
build: build,
run: run,
runInteractive: require('./lib/run-interactive'),
runLinked: runLinked,
inspect: inspect,
stop: stop,
Expand Down
101 changes: 101 additions & 0 deletions lib/run-interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var Docker = require('dockerode')
var docker = new Docker()

var previousKey
var CTRL_P = '\u0010'
var CTRL_Q = '\u0011'

function handleError (err) {
console.log(err)
process.exit(1)
}

function handler (err, container) {
var attachOpts = {stream: true, stdin: true, stdout: true, stderr: true}
if (err) handleError(err)
container.attach(attachOpts, function handler (err, stream) {
if (err) handleError(err)
// Show outputs
stream.pipe(process.stdout)

// Connect stdin
var isRaw = process.isRaw
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.setRawMode(true)
process.stdin.pipe(stream)

process.stdin.on('data', function (key) {
// Detects it is detaching a running container
if (previousKey === CTRL_P && key === CTRL_Q) exit(stream, isRaw)
previousKey = key
})

container.start(function (err, data) {
if (err) handleError(err)
resize(container)
process.stdout.on('resize', function () {
resize(container)
})

container.wait(function (err, data) {
if (err) handleError(err)
exit(stream, isRaw)
})
})
})
}

// Resize tty
function resize (container) {
var dimensions = {
h: process.stdout.rows,
w: process.stderr.columns
}

if (dimensions.h !== 0 && dimensions.w !== 0) {
container.resize(dimensions, function () {})
}
}

// Exit container
function exit (stream, isRaw) {
process.stdout.removeListener('resize', resize)
process.stdin.removeAllListeners()
process.stdin.setRawMode(isRaw)
process.stdin.resume()
stream.end()
process.exit()
}

function runInteractive (params) {
if (params.command && !Array.isArray(params.command)) {
params.command = params.command.split(' ')
}
if (params.entrypoint && !Array.isArray(params.entrypoint)) {
params.entrypoint = params.entrypoint.split(' ')
}
var optsc = {
Hostname: '',
User: '',
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
OpenStdin: true,
StdinOnce: false,
Env: params.env || null,
Entrypoint: params.entrypoint,
Cmd: params.cmd,
Image: params.image,
HostConfig: {
Links: params.links,
Binds: params.binds
},
Volumes: {},
VolumesFrom: []
}
docker.createContainer(optsc, handler)
}

module.exports = runInteractive
7 changes: 7 additions & 0 deletions test/interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

var docker = require('..')

docker.runInteractive({
image: 'alantrrs/standalone-test',
entrypoint: 'sh'
})