-
-
Save Bostonncity/3e6f07139ce244d5f4b1 to your computer and use it in GitHub Desktop.
Get docker container ip and port
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
// sudo docker run -it --rm -p 3000 -v /var/run/docker.sock:/var/run/docker.sock rwwa/test_service | |
var http = require('http'); | |
var hostname = process.env['HOSTNAME']; | |
console.log('hostname: ' + hostname); | |
function getContainerInfo() { | |
var options = { | |
socketPath: '/var/run/docker.sock', | |
method: 'GET', | |
path: '/containers/' + hostname + '/json' | |
}; | |
var req = http.request(options, function(res) { | |
var response = ''; | |
res.on('error', function() { | |
console.log('error...'); | |
}); | |
res.on('data', function(chunk) { | |
console.log('receiving data...'); | |
response += chunk; | |
}); | |
res.on('end', function() { | |
console.log('closing...'); | |
return JSON.parse(response); | |
}); | |
}); | |
req.end(); | |
} | |
function extractIpPort(containerInfo) { | |
var ip = containerInfo['NetworkSettings']['IPAddress']; | |
var port = containerInfo['NetworkSettings']['Ports']['3000/tcp']['HostPort']; | |
return { | |
ip: ip, | |
port: port | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment