Start simple Docker container in Detached(Background) mode
docker run -d --name test ubuntu tail -f /dev/null
Here comes my simple golang code where I connect to running container. In current connection I want to get functional tty.
package main
import (
"fmt"
"os/exec"
"bufio"
"io"
"os"
"github.com/kr/pty"
)
func main() {
cmd := exec.Command("docker", "exec", "-it", "test", "bin/bash")
tty, err := pty.Start(cmd)
if err != nil {
fmt.Println("Error start cmd", err)
}
defer tty.Close()
go func() {
scanner := bufio.NewScanner(tty)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
go func() {
io.Copy(tty, os.Stdin)
}()
err = cmd.Wait()
if err != nil {
fmt.Println("Error Waiting", err)
}
}
More less it works, but there is couple thinks which is not working as I would run docker command from my terminal.
root@ba351b44ca80:/#
only after hitting return it appears, but my currsor is in new line where are no prompt;Also arrow up to get previous command is not working Only prints out
root@ba351b44ca80:/#
^[[A^[[A^[[A
but behind scene previous command is selected and by hitting return it is executed.
After executing command for cursor is not displayed prompt, like
root@ba351b44ca80:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
<Here my cursor>
go-dockerclient is worth checking out. It is a simple nice abstraction of the Docker remote API. It is used by many opensource projects and is regularly maintained as well.