Skip to content

Instantly share code, notes, and snippets.

@DavesCodeMusings
Created January 7, 2022 04:48
Show Gist options
  • Select an option

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

Select an option

Save DavesCodeMusings/357805339cdce7b2670d56c336febb6d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
import { request } from 'http';
// Replace containerID below.
const containerID = 'abcdefg1234567890hijklmnop1234567890qrstuv1234567890wxyz12345678';
function startExec(execID) {
let options = {
socketPath: '/var/run/docker.sock',
method: 'POST',
path: `/exec/${execID}/start`,
headers: { "Content-Type": "application/json" },
};
const req = request(options, (res) => {
let data = '';
res.on('data', d => {
data += d.toString();
});
res.on('end', () => {
console.log(`${res.statusCode}: ${data}`);
});
});
req.on('error', err => {
console.error(err);
});
req.write('{ "Detach": false, "Tty": true }');
req.end();
}
function createExec(containerID) {
let options = {
socketPath: '/var/run/docker.sock',
method: 'POST',
path: `/containers/${containerID}/exec`,
headers: { "Content-Type": "application/json" },
};
const req = request(options, (res) => {
let data = '';
res.on('data', d => {
data += d.toString();
});
res.on('end', () => {
console.log(`${res.statusCode}: ${data}`);
let execID = JSON.parse(data).Id;
startExec(execID);
});
});
req.on('error', err => {
console.error(err);
});
req.write('{ "Tty": true, "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Cmd": [ "date", "--iso-8601=seconds" ] }');
req.end();
}
createExec(containerID);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment