dongnao3990 2018-02-10 18:32
浏览 135

docker run -w从go脚本运行时出现意外错误

I'm writing a very simple script that just formats a build command via docker for go apps. It formats the command like this:

docker run --rm -v c:/Users/me/go/src/goapp:/go/src/goapp -w /go/src/goapp -e GOOS=os -e GOARCH=arch image go build -v -o outputname

When running this, however I get the following:

docker: Error response from daemon: the working directory ' /go/src/goapp' is invalid, it needs to be an absolute path

I tried reformatting it like this:

docker run --rm -v="c:/Users/me/go/src/goapp:/go/src/goapp" -w="/go/src/goapp" -e="GOOS=os" -e"GOARCH=arch" image go build -v -o outputname

and get the same error, only the "invalid" working directory is "/go/src/goapp"

Here's my code:

package main

import (

func constructCmd(volumeMap, workingDir, goos, goarch, output, image string) []string {
    finalCmd := append([]string{"run", "--rm"},
        fmt.Sprintf("-v='%s'", volumeMap),
        fmt.Sprintf("-w='%s'", workingDir),
        fmt.Sprintf("-e='%s'", goos),
        fmt.Sprintf("-e='%s'", goarch),

    finalCmd = append(finalCmd, image, "go build -v")

    if output != "" {
        finalCmd = append(finalCmd, fmt.Sprintf("-o %s", output))

    return finalCmd

func main() {
    // Parse flags
    osPtr := flag.String("os", "windows", "Target distribution")
    archPtr := flag.String("arch", "amd64", "Target distribution")
    outputPtr := flag.String("out", "", "Output file name")

    fmt.Printf("Building for %s/%s:
", *osPtr, *archPtr)

    goos := "GOOS=" + *osPtr
    goarch := "GOARCH=" + *archPtr

    pwd, _ := os.Getwd()
    if runtime.GOOS == "windows" {
        pwd = strings.Replace(pwd, "C", "c", 1)
        pwd = strings.Replace(pwd, "\\", "/", -1)
    workingDir := pwd[strings.Index(pwd, "/go"):]
    volumeMap := fmt.Sprintf("%s:%s", pwd, workingDir)

    var image string
    if len(flag.Args()) == 0 {
        image = "golang"
    } else {
        image = flag.Args()[0]

    execCmd := constructCmd(volumeMap, workingDir, goos, goarch, *outputPtr, image)

    cmd := exec.Command("docker", execCmd...)
    cmdOutput := &bytes.Buffer{}
    cmd.Stdout = cmdOutput
    cmd.Stderr = cmdOutput
    err := cmd.Run()
    if err != nil {

To note, if I run this command directly, it works no problem. No error. So this isn't a docker problem, it's a go problem. What could cause this error?

  • 写回答

1条回答 默认 最新

  • doumi4676 2018-02-10 21:37

    Remove the quoted quotes. As stated in the exec package's documentation, Go's standard lib os/exec package does not invoke the shell to execute a Cmd:

    Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells. The package behaves more like C's "exec" family of functions.

    Unless you're calling the shell in your command, there's no shell being invoked to process and remove those quotes.

    For example, this:


    Should be this:


    Update from comments: OP also found and fixed a second issue:

    "go build -v"

    needed to be split up into separate arguments:

    "go", "build", "-v"
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥100 vs2019 mfc程序如何实现64*64/48*48大小的真彩色工具栏
  • ¥15 全志v3s耳机音频输出口怎么外接功放
  • ¥15 华为ensp使用基本ACL限制公司网络访问
  • ¥15 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?
  • ¥15 谷歌地图是不是不开通结算功能,api会使用不了哦
  • ¥15 unity腾讯云对象存储机型适配
  • ¥15 求全国交通咨询模拟代码,要求如下,可以完全在dev c++运行
  • ¥15 根据要求修改程序编码
  • ¥15 用 Python 做一个用 Excel 表导入的答题系统