gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:14:42 26 4
gpt4 key购买 nike

我正在尝试使用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

最佳答案

这可以通过以下配置解决:

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

关于docker - Docker Go lang SDK从ContainerExecCreate不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59786519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com