执行使用Golang exec的Docker命令失败。

huangapple go评论72阅读模式
英文:

Executing docker command using golang exec fails

问题

我正在使用cmd.go(见下文)执行一个docker命令,但是失败了。我按照以下步骤执行并得到以下错误。

go build
sudo ./cmd

输出:

docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m
2014/10/16 14:32:12 退出状态 1

另一方面,直接运行

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

会得到正确的输出。

Hello World

这是cmd.go的代码。我该如何使其工作?谢谢!

package main

import (
        "fmt"
        "log"
        "os/exec"
        "strings"
)

func ExampleCmd_Output() {
        //out, err := exec.Command("date", "--version").Output()   // 这个可以工作
        //out, err := exec.Command("docker", "--version").Output() // 这个可以工作
        //out, err := exec.Command(cmd, "images").Output() // 即使是docker images命令也可以工作!

        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()
}

编辑:根据评论,我尝试执行命令"docker images"。如果我使用sudo运行可执行文件,它可以工作。也就是说,我现在在代码中使用以下行。

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

在执行go build并运行"sudo ./cmd"之后,我得到了docker images命令的输出。然而,如果没有sudo,我仍然得到退出状态1。但是,即使使用sudo,使用上面的docker run命令,我也没有得到输出。

英文:

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.

答案1

得分: 25

感谢https://stackoverflow.com/questions/24095661/os-exec-sudo-command-in-go,现在我能够做我想做的事情了。

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)
}

输出:

Hello World
英文:

Thanks to https://stackoverflow.com/questions/24095661/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

huangapple
  • 本文由 发表于 2014年10月17日 02:40:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/26411594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定