Skip to content

Instantly share code, notes, and snippets.

@DavesCodeMusings
Created January 9, 2022 23:37
Show Gist options
  • Select an option

  • Save DavesCodeMusings/be07ec8c1fd692d1037c910754d8497f to your computer and use it in GitHub Desktop.

Select an option

Save DavesCodeMusings/be07ec8c1fd692d1037c910754d8497f to your computer and use it in GitHub Desktop.
Call the Docker API using http.request wrapped in a promise
#!/usr/bin/env node
import { request } from 'http';
function callDockerAPI(path) {
return new Promise ((resolve, reject) => {
let options = {
socketPath: '/var/run/docker.sock',
method: 'GET',
path: path
};
const req = request(options, (res) => {
let data = '';
res.on('data', d => {
data += d.toString();
});
res.on('end', () => {
console.info(`${res.statusCode} API ${options.method} ${options.path}`);
resolve(data);
});
});
req.on('error', err => {
reject(err);
});
req.end();
});
}
let info = await callDockerAPI('/info');
console.log(info);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment