Trying to run terminal commands in Go using exec to get docker network usage but unable to. The following link shows how to get docker container's network usage using terminal and it works fine in terminal but not using Go. https://docs.docker.com/config/containers/runmetrics/
I get exit code 1, 2, 125, etc. with different combinations.
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
defer stdin.Close()
io.WriteString(stdin, "CID="+CID) // container ID
io.WriteString(stdin,"TASKS=/sys/fs/cgroup/devices/docker/$CID*/tasks")
io.WriteString(stdin, "PID=$(head -n 1 $TASKS)")
io.WriteString(stdin, "mkdir -p /var/run/netns")
io.WriteString(stdin, "ln -sf /proc/$PID/ns/net /var/run/netns/$CID")
io.WriteString(stdin, "ip netns exec $CID netstat -i")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s
", out)
Expected output:
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1450 1228323 0 0 0 1761314 0 0 0 BMRU