$ -weight: 500;">docker run -it ubuntu /bin bash
-weight: 500;">docker run -it ubuntu /bin bash
-weight: 500;">docker run -it ubuntu /bin bash
go run main.go run /bin/bash
go run main.go run /bin/bash
go run main.go run /bin/bash
package main import ( "fmt" "os"
) func main() { // os.Args is a list of everything typed in the terminal. // [0] is the program name (main.go), [1] is our command ("run") switch os.Args[1] { case "run": run() default: panic("Invalid argument") }
} func run() { // os.Args[2:] takes everything AFTER the "run" command // e.g., ["echo", "hello", "world"] fmt.Printf("Running %v \n", os.Args[2:])
}
package main import ( "fmt" "os"
) func main() { // os.Args is a list of everything typed in the terminal. // [0] is the program name (main.go), [1] is our command ("run") switch os.Args[1] { case "run": run() default: panic("Invalid argument") }
} func run() { // os.Args[2:] takes everything AFTER the "run" command // e.g., ["echo", "hello", "world"] fmt.Printf("Running %v \n", os.Args[2:])
}
package main import ( "fmt" "os"
) func main() { // os.Args is a list of everything typed in the terminal. // [0] is the program name (main.go), [1] is our command ("run") switch os.Args[1] { case "run": run() default: panic("Invalid argument") }
} func run() { // os.Args[2:] takes everything AFTER the "run" command // e.g., ["echo", "hello", "world"] fmt.Printf("Running %v \n", os.Args[2:])
}
$ go run main.go run echo hello world
Running [echo hello world]
$ go run main.go run echo hello world
Running [echo hello world]
$ go run main.go run echo hello world
Running [echo hello world]
package main import ( "fmt" "os" "os/exec"
) func main() { switch os.Args[1] { case "run": run() default: panic("Invalid argument") }
} func run() { fmt.Printf("Running %v \n", os.Args[2:]) // 1. Define the command we want to execute. This includes the third element in our array // (the actual command, in our case "echo"), as well as any optional arguments we may // want to pass to it ("hello world"). cmd := exec.Command(os.Args[2], os.Args[3:]...) // 2. Wire up the plumbing cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr // 3. Run the command and handle any errors must(cmd.Run())
} // A tiny helper function to catch errors and crash the program // cleanly if something goes wrong.
func must(err error) { if err != nil { panic(err) }
}
package main import ( "fmt" "os" "os/exec"
) func main() { switch os.Args[1] { case "run": run() default: panic("Invalid argument") }
} func run() { fmt.Printf("Running %v \n", os.Args[2:]) // 1. Define the command we want to execute. This includes the third element in our array // (the actual command, in our case "echo"), as well as any optional arguments we may // want to pass to it ("hello world"). cmd := exec.Command(os.Args[2], os.Args[3:]...) // 2. Wire up the plumbing cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr // 3. Run the command and handle any errors must(cmd.Run())
} // A tiny helper function to catch errors and crash the program // cleanly if something goes wrong.
func must(err error) { if err != nil { panic(err) }
}
package main import ( "fmt" "os" "os/exec"
) func main() { switch os.Args[1] { case "run": run() default: panic("Invalid argument") }
} func run() { fmt.Printf("Running %v \n", os.Args[2:]) // 1. Define the command we want to execute. This includes the third element in our array // (the actual command, in our case "echo"), as well as any optional arguments we may // want to pass to it ("hello world"). cmd := exec.Command(os.Args[2], os.Args[3:]...) // 2. Wire up the plumbing cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr // 3. Run the command and handle any errors must(cmd.Run())
} // A tiny helper function to catch errors and crash the program // cleanly if something goes wrong.
func must(err error) { if err != nil { panic(err) }
}
$ go run main.go run echo hello world
Running [echo hello world]
hello world
$ go run main.go run echo hello world
Running [echo hello world]
hello world
$ go run main.go run echo hello world
Running [echo hello world]
hello world
$ go run main.go run /bin/bash
Running [/bin/bash]
root@your-computer:/home/yechiel/-weight: 500;">docker-clone#
$ go run main.go run /bin/bash
Running [/bin/bash]
root@your-computer:/home/yechiel/-weight: 500;">docker-clone#
$ go run main.go run /bin/bash
Running [/bin/bash]
root@your-computer:/home/yechiel/-weight: 500;">docker-clone#
func run() { 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 // Set up the namespace! cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS, } must(cmd.Run())
}
func run() { 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 // Set up the namespace! cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS, } must(cmd.Run())
}
func run() { 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 // Set up the namespace! cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS, } must(cmd.Run())
}
package main import ( "fmt" "os" "os/exec" "syscall"
) func main() { switch os.Args[1] { case "run": run() case "child": child() // We added a new command here! default: panic("Invalid argument") }
} func run() { fmt.Printf("Running %v \n", os.Args[2:]) // /proc/self/exe is a special Linux file that points to the program // currently running. So, our program is calling itself! // We pass "child" as the first argument, followed by the rest of our commands. args := append([]string{"child"}, os.Args[2:]...) cmd := exec.Command("/proc/self/exe", args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS, } must(cmd.Run())
} func child() { fmt.Printf("Running in new child process %v \n", os.Args[2:]) // We are now inside the namespace! It is safe to change the hostname. must(syscall.Sethostname([]byte("container"))) // Now we run the actual command the user requested (like /bin/bash) cmd := exec.Command(os.Args[2], os.Args[3:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr must(cmd.Run())
} func must(err error) { if err != nil { panic(err) }
}
package main import ( "fmt" "os" "os/exec" "syscall"
) func main() { switch os.Args[1] { case "run": run() case "child": child() // We added a new command here! default: panic("Invalid argument") }
} func run() { fmt.Printf("Running %v \n", os.Args[2:]) // /proc/self/exe is a special Linux file that points to the program // currently running. So, our program is calling itself! // We pass "child" as the first argument, followed by the rest of our commands. args := append([]string{"child"}, os.Args[2:]...) cmd := exec.Command("/proc/self/exe", args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS, } must(cmd.Run())
} func child() { fmt.Printf("Running in new child process %v \n", os.Args[2:]) // We are now inside the namespace! It is safe to change the hostname. must(syscall.Sethostname([]byte("container"))) // Now we run the actual command the user requested (like /bin/bash) cmd := exec.Command(os.Args[2], os.Args[3:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr must(cmd.Run())
} func must(err error) { if err != nil { panic(err) }
}
package main import ( "fmt" "os" "os/exec" "syscall"
) func main() { switch os.Args[1] { case "run": run() case "child": child() // We added a new command here! default: panic("Invalid argument") }
} func run() { fmt.Printf("Running %v \n", os.Args[2:]) // /proc/self/exe is a special Linux file that points to the program // currently running. So, our program is calling itself! // We pass "child" as the first argument, followed by the rest of our commands. args := append([]string{"child"}, os.Args[2:]...) cmd := exec.Command("/proc/self/exe", args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS, } must(cmd.Run())
} func child() { fmt.Printf("Running in new child process %v \n", os.Args[2:]) // We are now inside the namespace! It is safe to change the hostname. must(syscall.Sethostname([]byte("container"))) // Now we run the actual command the user requested (like /bin/bash) cmd := exec.Command(os.Args[2], os.Args[3:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr must(cmd.Run())
} func must(err error) { if err != nil { panic(err) }
}
$ -weight: 600;">sudo go run main.go run /bin/bash
Running [/bin/bash]
Running in new child process [/bin/bash]
root@container:/home/yechiel/-weight: 500;">docker-clone#
$ -weight: 600;">sudo go run main.go run /bin/bash
Running [/bin/bash]
Running in new child process [/bin/bash]
root@container:/home/yechiel/-weight: 500;">docker-clone#
$ -weight: 600;">sudo go run main.go run /bin/bash
Running [/bin/bash]
Running in new child process [/bin/bash]
root@container:/home/yechiel/-weight: 500;">docker-clone# - Your prompt: Everyone's prompt is different, but most have something in the beginning that looks like [username]@[hostname]. The prompt you see now probably looks like root@[some-random-string].
- Your hostname: If you type hostname in your host computer's terminal, it will output your computer's actual name. If you run hostname inside your container, on the other hand, you will see that same random sequence of characters from your prompt. Docker assigned this fake hostname to your container at random.
- Your File System: If you type ls / inside your container you will see that none of the files from your computer's actual root directory are there, instead you will see a fresh list of files and directories, exactly like you would find in a brand new Ubuntu installation.
- Your processes: If you type ps aux in your container you will find only 2 processes with very low Process IDs (PIDs)—typically PID 1 for the shell process you're in and another low number PID for the ps command you just ran. Meanwhile, running ps aux on your host machine will show a massive list of running processes, many of them with very high PIDs. - We will see a different hostname than our existing one.
- The root directory we will have access to will not be the root directory of our computer, instead it will be a new root directory.
- If we inspect the processes running using ps we will see only the processes running in our program and not all the processes on our computer. - Define the command: exec.Command takes the program we want to run (like echo) and any arguments we want to pass to it (hello world).
- Wire up the plumbing: This part is crucial. By default, when a program spins up a new process, it runs invisibly in the background. By pointing the new command's Standard Input, Output, and Error to our own os.Stdin, os.Stdout, and os.Stderr, we are attaching its "mouth and ears" directly to our terminal so we can actually interact with it.
- Run it: We execute the command. Because Go requires explicit error handling, we wrapped it in a tiny must() helper function to keep our code readable. - "/proc/self/exe": In Linux, /proc is a special directory that holds information about all running processes. If you check what's in there (by running ls /proc) you will see a whole bunch of directories that have numbers as names. Each number is a PID and that directory contains information about the process with that PID. /proc/self/exe is essentially a shortcut to the directory for the process that's currently running (in our case go run main.go).
- args...: We are taking the word "child" and appending the rest of the user's commands (like /bin/bash) to it. The ... syntax in Go simply "unpacks" the list so it can be passed as individual arguments. - The program starts, sees "run", and triggers the run() function.
- The run() function sets up the invisibility cloak (the NEWUTS namespace).
- Inside that cloak, it runs itself again, but this time passing the command "child".
- The program starts a second time, sees "child", and triggers the child() function.
- Because we are now safely inside the namespace, it changes the hostname to container and finally runs /bin/bash.