Created
December 31, 2013 00:03
-
-
Save mplewis/8190309 to your computer and use it in GitHub Desktop.
doodling with node.js <—> Docker REST socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var SOCKET_PATH = '/var/run/docker.sock'; | |
var dockerConn = { | |
get: function(path, callback) { | |
var options = { | |
socketPath: SOCKET_PATH, | |
path: path | |
}; | |
http.get(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(json) { | |
var data = JSON.parse(json); | |
var response = { | |
'status_code': res.statusCode, | |
'data': data | |
}; | |
callback(null, response); | |
}); | |
}).on('error', function(err) { | |
callback(err, null); | |
}); | |
}, | |
post: function(path, data, callback) { | |
var jsonData = JSON.stringify(data); | |
var headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': jsonData.length | |
}; | |
var options = { | |
socketPath: SOCKET_PATH, | |
path: path, | |
method: 'POST', | |
headers: headers | |
}; | |
var req = http.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(data) { | |
var response = { | |
'status_code': res.statusCode, | |
'data': data | |
}; | |
callback(null, response); | |
}); | |
}).on('error', function(err) { | |
callback(err, null); | |
}); | |
req.write(jsonData); | |
req.end(); | |
} | |
}; | |
var dockerApi = { | |
images: function(callback) { | |
dockerConn.get('/images/json', callback); | |
}, | |
containers: function(callback) { | |
dockerConn.get('/containers/json', callback); | |
}, | |
container: { | |
create: function(data, callback) { | |
dockerConn.post('/containers/create', data, callback); | |
} | |
} | |
}; | |
dockerApi.images(function(err, data) { | |
if (err) { | |
console.log('Error getting images.'); | |
throw err; | |
} else { | |
console.log('IMAGES:'); | |
console.log(data); | |
} | |
}); | |
dockerApi.containers(function(err, data) { | |
if (err) { | |
console.log('Error getting containers.'); | |
throw err; | |
} else { | |
console.log('CONTAINERS:'); | |
console.log(data); | |
} | |
}); | |
var toCreate = { | |
'Hostname': '', | |
'User': '', | |
'Memory': 0, | |
'MemorySwap': 0, | |
'AttachStdin': false, | |
'AttachStdout': true, | |
'AttachStderr': true, | |
'PortSpecs': null, | |
'Privileged': false, | |
'Tty': false, | |
'OpenStdin': true, | |
'StdinOnce': true, | |
'Env': null, | |
'Cmd': ['/bin/sh', '-c "while true; do echo hello world; sleep 1; done"'], | |
'Dns': null, | |
'Image': 'ubuntu', | |
'Volumes': {}, | |
'VolumesFrom': '', | |
'WorkingDir': '' | |
}; | |
dockerApi.container.create(toCreate, function(err, data) { | |
if (err) { | |
console.log('Error creating container.'); | |
throw err; | |
} else { | |
console.log('RESULTS OF CONTAINER CREATION:'); | |
console.log(data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment