Skip to content

Instantly share code, notes, and snippets.

@cmingxu
Created March 2, 2016 11:55
Show Gist options
  • Save cmingxu/c20d40fe1b6b8b9f2ebc to your computer and use it in GitHub Desktop.
Save cmingxu/c20d40fe1b6b8b9f2ebc to your computer and use it in GitHub Desktop.
xxx
package main
import (
"bufio"
"fmt"
docker "github.com/fsouza/go-dockerclient"
"io"
"time"
)
var containerId = "b973151dcdd9"
var execCommand = []string{"tail", "-f", "/var/log/customize_log.log"}
//var execCommand = []string{"cat", "-n", "/etc/hosts"}
var exec *docker.Exec
var success = make(chan struct{}, 1)
var chLine = make(chan string)
func main() {
client, e := docker.NewClientFromEnv()
if e != nil {
panic(e)
}
exec, err := client.CreateExec(docker.CreateExecOptions{
Cmd: execCommand,
Container: containerId,
AttachStdin: false,
AttachStdout: true,
})
if err != nil {
panic(err)
}
rd, wd := io.Pipe()
defer rd.Close()
defer wd.Close()
_, err = client.StartExecNonBlocking(exec.ID,
docker.StartExecOptions{
OutputStream: wd,
ErrorStream: wd,
})
if err != nil {
panic(err)
}
time.Sleep(time.Second)
go func() {
for {
fmt.Println("x")
tmp, _ := client.InspectExec(exec.ID)
if !tmp.Running {
fmt.Println("not running")
break
} else {
fmt.Println("running")
buf := bufio.NewReader(rd)
fmt.Println(buf.Buffered())
for {
l, _ := buf.ReadString('\n')
fmt.Println(l)
}
}
time.Sleep(time.Second * 3)
}
}()
<-success
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment