Skip to content

Instantly share code, notes, and snippets.

@echoz
Last active November 14, 2020 08:48
Show Gist options
  • Save echoz/5097eb1add303adef771f0d271cbe4d1 to your computer and use it in GitHub Desktop.
Save echoz/5097eb1add303adef771f0d271cbe4d1 to your computer and use it in GitHub Desktop.
Monitor Docker Engine (Node JS)
let http = require('http')
const options = {
socketPath: "/var/run/docker.sock",
path: "/events?type=container"
}
const request = http.request(options, (res) => {
res.on('data', (data) => {
let json = JSON.parse(data.toString())
const actor = json["Actor"]
const action = json["Action"]
if (action == "start" || action == "update") {
const options = {
socketPath: "/var/run/docker.sock",
path: "/containers/" + actor["ID"] + "/json"
}
const request = http.request(options, (res) => {
res.on('data', (data) => {
let json = JSON.parse(data.toString())
const networkMode = json["HostConfig"]["NetworkMode"]
const networks = json["NetworkSettings"]["Networks"]
const networkConfig = networks[networkMode]
console.log(json)
console.log("Network: " + JSON.stringify(networkConfig, undefined, 2))
})
})
request.end()
}
})
})
request.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment