Skip to content

Instantly share code, notes, and snippets.

@UlisseMini
Created June 2, 2020 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UlisseMini/deb1b02691a73909b98da8270e5d2d2e to your computer and use it in GitHub Desktop.
Save UlisseMini/deb1b02691a73909b98da8270e5d2d2e to your computer and use it in GitHub Desktop.
const Docker = require('dockerode')
const docker = new Docker()
const stripAnsi = require("strip-ansi");
// docker run --cpus=0.1 --memory=64m --kernel-memory=64m --ulimit="nproc=128:128" -i alpine:latest sh -c "$*"
const cmd = process.argv.join(' ')
const kb = 1024
const mb = kb * 1024
limits = {
HostConfig: {
// prevent container from spawning more then 64 processes (mitigate fork bomb)
PidsLimit: 128,
// cap cpu per container at 10% of one core
// NanoCpus: 1e9,
// limit memory so they can't consume it all
Memory: mb * 64,
}
}
;(async () => {
const container = await docker.createContainer(
{
Image: 'alpine:latest',
// Tty: true,
Cmd: [ "sh", "-c", "f() { f|f& }; while true; do f &; done" ],
...limits
})
const options = {
Cmd: [ "sh", "-c", "f() { f|f& }; while true; do f &; done" ],
// AttachStdout: true,
// AttachStderr: true
};
await container.start()
console.log('start:', container.id)
const exec = await container.exec(options)
const started = await exec.start()
await new Promise(resolve => setTimeout(resolve, 10000))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment