9

I am using cmd.go (see below) to execute a docker command but it fails. I do the following steps to execute and get the following error.

go build
sudo ./cmd

Output:

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 running directly as

sudo docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m

results in the correct output of a.out.

Hello World

This is the code of cmd.go. How can I get it to work? Thanks!

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 a comment, I tried executing the command "docker images". It works if I run the executable with sudo. That is, I am using the following line in the code now.

out, err := exec.Command(cmd, "images").Output()

After doing go build and running "sudo ./cmd", I get the output of docker images command. However, without sudo, I still get exit status 1. But with docker run command above even with sudo, I don't get an output.

Madhav Jha
  • 863
  • 1
  • 8
  • 26
  • does running `docker images` produces the expected results even when not using sudo? – Thomasleveil Oct 16 '14 at 19:50
  • Yes, docker images command work! (It only works when I run the generated go executable with sudo but not otherwise.) I made edits above in the question. – Madhav Jha Oct 16 '14 at 20:26
  • I mean without using your go program. I.E: type `$ docker image` in the console. Does it work without the sudo? – Thomasleveil Oct 16 '14 at 20:48
  • No, it doesn't work without sudo in console.Error: 2014/10/16 16:56:05 Get http:///var/run/docker.sock/v1.14/images/json: dial unix /var/run/docker.sock: permission denied – Madhav Jha Oct 16 '14 at 20:55
  • then add your current user to the `docker` group and open a new shell and you won't need sudo anymore. See http://askubuntu.com/questions/477551/how-can-i-use-docker-without-sudo – Thomasleveil Oct 16 '14 at 20:56
  • possible duplicate of [Os Exec Sudo Command in Go](http://stackoverflow.com/questions/24095661/os-exec-sudo-command-in-go) – Thomasleveil Oct 16 '14 at 21:02
  • Hi Thomas. I did it and now I don't need to use sudo. Still I am not able to execute following docker command from go program's exec: docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m. (Now I am able to run docker images from go program without sudo.) – Madhav Jha Oct 17 '14 at 00:00

1 Answers1

25

Thanks to Os Exec Sudo Command in Go, I am now able to do what I want.

func main() {
  cmdStr := "sudo docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m"
  out, _ := exec.Command("/bin/sh", "-c", cmdStr).Output()  
  fmt.Printf("%s", out)
}

Output:

Hello World
Community
  • 1
  • 1
Madhav Jha
  • 863
  • 1
  • 8
  • 26