Created
January 9, 2022 23:37
-
-
Save DavesCodeMusings/be07ec8c1fd692d1037c910754d8497f to your computer and use it in GitHub Desktop.
Call the Docker API using http.request wrapped in a promise
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
| #!/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