简体   繁体   English

Docker Go lang SDK 从 ContainerExecCreate 不返回任何内容

[英]Docker Go lang SDK returns nothing from ContainerExecCreate

I am trying to execute a command (let's say "pwd) using Docker Go lang SDK, and I expect it returns the working directory on the container. But it returns nothing back. I am not sure what is the issue.我正在尝试使用 Docker Go lang SDK 执行一个命令(比如“pwd”),我希望它返回容器上的工作目录。但它什么都不返回。我不确定是什么问题。

    rst, err := cli.ContainerExecCreate(context.Background(), "0df7c1d9d185b1da627efb983886a12fefc32120d035b34e97c3ad13da6dd9cc", types.ExecConfig{Cmd: []string{"pwd"}})
if err != nil {
    panic(err)
}

//res, err := cli.ContainerExecInspect(context.Background(), rst.ID)
//print(res.ExitCode)

response, err := cli.ContainerExecAttach(context.Background(), rst.ID, types.ExecStartCheck{})
if err != nil {
    panic(err)
}
defer response.Close()

data, _ := ioutil.ReadAll(response.Reader)
fmt.Println(string(data))


GOROOT=/usr/local/Cellar/go/1.13.5/libexec #gosetup
GOPATH=/Users/pt/go #gosetup
/usr/local/Cellar/go/1.13.5/libexec/bin/go build -o /private/var/folders/yp/hh3_03d541x0r6t7_zwqqhqr0000gn/T/___go_build_main_go /Users/pt/go/src/awesomeProject/main.go #gosetup
/private/var/folders/yp/hh3_03d541x0r6t7_zwqqhqr0000gn/T/___go_build_main_go #gosetup

### It does not print the working directory ###
Process finished with exit code 0

This is solved by the following config:这是通过以下配置解决的:

optionsCreate := types.ExecConfig{
    AttachStdout: true,
    AttachStderr: true,
    Cmd:          []string{"ls", "-a"},
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM