Skip to content

Instantly share code, notes, and snippets.

@skanehira
Last active April 29, 2019 01:58
Show Gist options
  • Save skanehira/cefae446112a4234d5cfbd55756c1675 to your computer and use it in GitHub Desktop.
Save skanehira/cefae446112a4234d5cfbd55756c1675 to your computer and use it in GitHub Desktop.
make a simple container
package main
import (
"os"
"os/exec"
"syscall"
)
func must(err error) {
if err != nil {
panic(err)
}
}
func main() {
// need to make rootfs dir from linux OS file system
// bellow is a sample to make rootfs dir from alpine
// $ docker pull alpine
// $ docker create --name alpine alpine
// $ docker export alpine > alpine.tar
// $ docker rm alpine
// $ mkdir rootfs
must(syscall.Chroot("./rootfs"))
must(syscall.Chdir("/"))
cmd := exec.Command("/bin/sh")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWUTS,
}
must(cmd.Run())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment