Docker API NodeJS
Make sure you mount the docker socket when you run the container i.e
docker run --name containera \ -v /var/run/docker.sock:/var/run/docker.sock \ yourimage
Give this a shot to list your containers:
let options = { socketPath: '/var/run/docker.sock', path: `/v1.26/containers/json`, method: 'GET' }; let clientRequest = http.request(options, (res) => { res.setEncoding('utf8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { const parsedData = JSON.parse(rawData); console.log(parsedData); }); }); clientRequest.on('error', (e) => { console.log(e); }); clientRequest.end();
While this can be done with request I would consider using Dockerode the Node SDK for Docker API as it handles the headers correctly and avoids issues like 400 Bad Request. Here is the NPM Link Dockerode NPMJS