Subscribe to events from a dockerode instance in a meaningful way
npm i -S docker-event-emitter
You can subscribe to:
- event: recieves all events
- <Type>: recieves all events for the selected Type (container, volume, network, ...)
- <Type>.<Action>: recieves all events for the seleced Type and Action
For a complete list of all available Types and Actions check the events section of the Docker API docs.
This example is pretty much self explanatory:
const Docker = require('dockerode');
const DockerEE = require('docker-event-emitter');
(async function main() {
// Setup a dockerode
const docker = new Docker({
socketPath: '/var/run/docker.sock',
});
// Setup the DockerEventEmitter
const events = new DockerEE(docker);
// Subscribe to events
events
.on('event', ev => {
// This will be triggered on any event
console.log(ev);
})
.on('container', ev => {
// This will be triggered on any Type='container' event
console.log(ev);
})
.on('network.connect', ev => {
// This will be triggered on any Type='network' and Action='connect' event
console.log(ev);
});
// Start recieving events
await events.start();
}).catch(err =>
console.log(err.message, err.stack) && process.exit(1)
);