dongtan2017 2019-02-18 12:53
浏览 1660

Golang Docker API:无法在容器上运行Exec

I am trying to run Exec Command on a docker container created via Go client for the Docker Engine API.

Here is my code.

func main() {
    ctx := context.Background()
    cli, err := client.NewEnvClient()
    if err != nil {
        panic(err)
    }

    reader, err := cli.ImagePull(ctx, "docker.io/library/centos:7", types.ImagePullOptions{})
    if err != nil {
        panic(err)
    }
    io.Copy(os.Stdout, reader)

    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: "centos",
        //Cmd:   []string{"cat", "/etc/hosts"},
        //Tty:   true,
    }, nil, nil, "")
    if err != nil {
         panic(err)
    }

    if err := cli.ContainerStart(ctx, resp.ID, 
types.ContainerStartOptions{}); err != nil {
        panic(err)
    }

     time.Sleep(5 * time.Second)

    c := types.ExecConfig{AttachStdout: true, AttachStderr: true, 
    Cmd: []string{"echo", "Hello Himanshu"}}
    execID, _ := cli.ContainerExecCreate(ctx, resp.ID, c)
    fmt.Println(execID)

    config := types.ExecConfig{}
    res, er := cli.ContainerExecAttach(ctx, execID.ID, config)
    if er != nil {
        fmt.Println("Some Error")
    }

    err = cli.ContainerExecStart(ctx,execID.ID,types.ExecStartCheck{})
    if err != nil {
        fmt.Println("Kuchh error")
    }
    content, _, _ := res.Reader.ReadLine()
    fmt.Println(string(content))
    }

Here I always get error:

cannot exec in a stopped state: unknown or

unable to upgrade to tcp, received 404

Even if I use container logs, it will print whatever is supplied in RUN cmd, but not what is sent in Exec. same error will be there as well.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
    • ¥15 WPF使用Canvas绘制矢量图问题
    • ¥15 用三极管设计一个单管共射放大电路
    • ¥15 孟德尔随机化r语言运行问题
    • ¥15 pyinstaller编译的时候出现No module named 'imp'
    • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
    • ¥15 怎么把多于硬盘空间放到根目录下
    • ¥15 Matlab问题解答有两个问题
    • ¥15 LCD12864中文显示
    • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决