dockerclient icon indicating copy to clipboard operation
dockerclient copied to clipboard

Howto: Run an exec command on a running container

Open beatscode opened this issue 9 years ago • 2 comments

Hello, I'm trying to run a command on a running container. I'm not sure what I'm doing wrong. Could you assist. Running the test below returns the following:

vagrant@vagrant-ubuntu-trusty-64:~/go/src/com.stuff/ambassador$ go test -test.run TestExec -v=== RUN TestExec
dac07107d20a5834826f16547945d5afe6b714fcefe0820fd64e5ea8b27b1aaf
--- PASS: TestExec (0.01s)
PASS
func TestExec(t *testing.T) {
    //var sContainer dockerclient.Container
    var config dockerclient.ExecConfig
    containers, err := docker.ListContainers(true, false, "")
    if err != nil {
        t.Fatalf("cannot get containers: %s", err)
    }
    for _, c := range containers {
        for _, name := range c.Names {
            if name == "/testapi" {
                config.Container = c.Id
            }
        }
    }
    if config.Container == "" {
        t.Error("Container Not found")
    }
    config.Cmd = []string{"bash", "-c", "date"}
    config.AttachStdout = true
    config.AttachStderr = true
    config.AttachStdin = false
    config.Tty = false
    config.Detach = false
    ID, err := docker.ExecCreate(&config)
    fmt.Println(ID)
    if err != nil {
        t.Error(err)
    }

    config.Cmd = []string{}
    config.AttachStdout = true
    config.AttachStderr = true
    config.AttachStdin = true
    config.Tty = true
    config.Detach = true
    //fmt.Print(config)
    err = docker.ExecStart(ID, &config)
    if err != nil {
        t.Error(err)
    }

    //fmt.Print(containers)
}

How do I run a command and trap the response?

beatscode avatar Oct 08 '15 20:10 beatscode

@beatscode did you figure this out? I am of need of this as well.

hekaldama avatar Nov 11 '15 16:11 hekaldama

Anyone know how to do this?

hekaldama avatar Nov 13 '15 17:11 hekaldama