0
votes

I am trying to access interactive shell of a running docker container using Golang. Here is what I tried.

package main

import (
  "fmt"
  "log"
  "os"
  "os/exec"
)

func main() {

    prg := "docker"

    arg1 := "exec"
    arg2 := "-ti"
    arg3 := "df43f9a0d5c4"
    arg4 := "bash"

    cmd := exec.Command(prg, arg1, arg2, arg3, arg4)
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    fmt.Printf("[Command] %s\n", cmd.String())
    log.Printf("Running command and waiting for it to finish...")
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Command finished with error %s\n", cmd.String())

}

AND here is output with error:

> [Command] /usr/bin/docker exec -ti df43f9a0d5c4 bash 2022/07/28
> 19:21:02 Running command and waiting for it to finish... the input
> device is not a TTY 2022/07/28 19:21:02 exit status 1 exit status 1

Note: Interactive shell of running docker container works fine when executing this command directly on the shell

What's your eventual goal with this? Could you use the "github.com/docker/docker/client" API client, instead of trying to shell to docker exec? If Docker weren't involved, how would you perform this task to run some command inside some other process? - David Maze