/ docker

Sample node.js application querying Docker daemon via unix socket

Here is a sample node application dockerclient.js which connects to the Docker daemon via unix socket. This checks services and shows how many replicas/tasks are running for each service.

const http = require('http');

const options = {
  socketPath: '/var/run/docker.sock',
  path: '/services'
};

const callback = resp => {
  console.log(`STATUS: ${resp.statusCode}`);

	var body = '';

    resp.on('data', (chunk) => {
	  body += chunk;
    }).on('end', () => {
        var respArr = JSON.parse(body + '');

        respArr.forEach((value) => {
            console.log(value.Spec.Name + ' replicas=' + value.Spec.Mode.Replicated.Replicas);

        })

    }).on('error', data => console.error(data));

}

const clientRequest = http.get(options, callback);
clientRequest.end(0);

This is just starter code which can get you up to speed. Share some more advanced usage and feel free to share in the comments section below.

Cheers!