Last active
November 14, 2020 08:48
-
-
Save echoz/5097eb1add303adef771f0d271cbe4d1 to your computer and use it in GitHub Desktop.
Monitor Docker Engine (Node JS)
This file contains hidden or 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
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