Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Created November 30, 2018 07:36
Show Gist options
  • Select an option

  • Save jreisinger/a01bb5fc40176078e6237649f2475fb6 to your computer and use it in GitHub Desktop.

Select an option

Save jreisinger/a01bb5fc40176078e6237649f2475fb6 to your computer and use it in GitHub Desktop.
// Must be run as root
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)
// go run mydocker.go run <cmd> <args>
func main() {
switch os.Args[1] {
case "run":
run()
case "child":
child()
default:
panic("help!")
}
}
func run() {
fmt.Printf("Running '%v'\n", os.Args[2:])
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Setup the hostname namespace
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS,
}
must(cmd.Run()) // we run the args here
}
func child() {
fmt.Printf("Running '%v'\n", os.Args[2:])
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
must(syscall.Sethostname([]byte("container")))
must(cmd.Run()) // we run the args here
}
func must(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment