I use cmd.go (see below) to execute the docker command, but it does not work. I follow the following steps to execute and get the following error.
go build
sudo ./cmd
Conclusion:
docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m
2014/10/16 14:32:12 exit status 1
On the other hand, it works like
sudo docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m
leads to the correct exit a.out.
Hello World
This is the cmd.go code. How can I make it work? Thank!
package main
import (
"fmt"
"log"
"os/exec"
"strings"
)
func ExampleCmd_Output() {
//out, err := exec.Command("date", "--version").Output() // This works
//out, err := exec.Command("docker", "--version").Output() // This works
//out, err := exec.Command(cmd, "images").Output() // Even docker images command works!
cmd := "docker"
cmdArgs := []string{"run", "-v", "~/exp/a.out:/a.out", "ubuntu:14.04", "/a.out", "-m", "10m"}
fmt.Println(cmd + " " + strings.Join(cmdArgs, " "))
out, err := exec.Command(cmd, cmdArgs...).Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
}
func main() {
ExampleCmd_Output()
}
EDIT: after the comment, I tried to execute the "docker image" command. It works if I run the executable using sudo. That is, I am using the following line in the code now.
out, err := exec.Command(cmd, "images").Output()
After completing the build and running "sudo./cmd", I get the output of the docker image command. However, without sudo, I still get exit status 1. But with the docker run command above, even with sudo, I don't get exit.