diff --git a/src/docker.js b/src/docker.js index 03dafba52..e5605b390 100644 --- a/src/docker.js +++ b/src/docker.js @@ -24,7 +24,8 @@ exports = module.exports = { deleteContainerByName: deleteContainer, deleteImage: deleteImage, deleteContainers: deleteContainers, - createSubcontainer: createSubcontainer + createSubcontainer: createSubcontainer, + getContainerIdByIp: getContainerIdByIp }; function connectionInstance() { @@ -355,3 +356,36 @@ function deleteImage(manifest, callback) { callback(error); }); } + +function getContainerIdByIp(ip, callback) { + assert.strictEqual(typeof ip, 'string'); + assert.strictEqual(typeof callback, 'function'); + + debug('get container by ip %s', ip); + + var docker = exports.connection; + + docker.listNetworks({}, function (error, result) { + if (error) return callback(error); + + var bridge; + result.forEach(function (n) { + if (n.Name === 'bridge') bridge = n; + }); + + if (!bridge) return callback(new Error('Unable to find the bridge network')); + + var containerId; + for (var id in bridge.Containers) { + if (bridge.Containers[id].IPv4Address.indexOf(ip) === 0) { + containerId = id; + break; + } + } + if (!containerId) return callback(new Error('No container with that ip')); + + debug('found container %s with ip %s', containerId, ip); + + callback(null, containerId); + }); +}