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.