Create a gist now

Instantly share code, notes, and snippets.

@jaytaylor /main.go
Last active Mar 29, 2017

What would you like to do?
Running Wireshark in Docker Containers
package main
// This is a cleaned up version of the code found in:
// Added:
// - Container ID passed as command-line argument
// - Error handling
import (
func main() {
if len(os.Args) == 1 {
fmt.Fprintln(os.Stderr, "error: missing required arg: container-id")
// Lock the OS Thread so we don’t accidentally switch namespaces.
defer runtime.UnlockOSThread()
// Save the current network namespace.
origns, err := netns.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting current network namespace: %s\n", err)
defer origns.Close()
// Get the network namespace based on container id.
newns, err := netns.GetFromDocker(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "error getting network namespace from docker: %s\n", err)
defer newns.Close()
// Do something with the network namespace.
ifaces, err := net.Interfaces()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting network interfaces: %s\n", err)
fmt.Printf("interfaces: %v\n", ifaces)
cmd := exec.Command("tshark", "-z", "conv,ip", "-i", "eth0")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error running cmd: %s\n", err)
// Return to original namespace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment