Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Howto: Run an exec command on a running container #173

Description

@beatscode

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions