go docker客户端每秒获取容器日志不返回任何内容
我正在使用go docker客户端每秒获取容器日志不返回任何内容,docker,go,Docker,Go,我正在使用docker.io/go-docker包启动一个带有go的容器。 一旦容器的主方法返回,我就能够获得容器的所有日志 if err := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil { panic(err) } statusCh, errCh = cli.ContainerWait(context.Background(), resp.I
docker.io/go-docker
包启动一个带有go的容器。
一旦容器的主方法返回,我就能够获得容器的所有日志
if err := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh = cli.ContainerWait(context.Background(), resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(context.Background(), resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
if err != nil {
panic(err)
}
// Do something with the logs here...
一切正常,除了ioutil.ReadAll(out)
不返回任何内容
我曾多次尝试使用时间格式,如,但仍然没有任何结果:
- nowUTC:=strconv.FormatInt(time.Now().UTC().UnixNano(),10)
- nowUTC:=strconv.FormatInt(time.Now().UTC().Unix(),10)
- nowUTC:=strconv.FormatInt(time.Now().UnixNano(),10)
- nowUTC:=strconv.FormatInt(time.Now().Unix(),10)
- nowUTC:=time.Now()格式(time.rfc339)
我错了什么?最后我使用
nowUTC:=time.Now().UTC()
使它工作起来,但问题不仅仅在于所使用的时间格式
诀窍是我在笔记本电脑上使用“Docker Machine”,每天晚上我都会关上笔记本电脑。每当笔记本电脑进入睡眠状态,Docker机器的内部时钟就会冻结
当笔记本电脑从睡眠中醒来时,它会导致笔记本电脑时钟和Docker机器时钟之间的时间漂移,而我的Docker机器迟到了x小时
我的Go代码运行到笔记本电脑上的CLI应用程序中,提取日志请求的时间标准与日志内容不匹配
重新启动docker机器后,一切正常
nowUTC := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
if err := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh = cli.ContainerWait(context.Background(), resp.ID, container.WaitConditionNotRunning)
exitCh := make(chan bool)
go func(since string, exit chan bool) {
Loop:
for {
select {
case <-exit:
break Loop
default:
sinceReq := since
time.Sleep(time.Second)
since = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
out, err := cli.ContainerLogs(context.Background(), resp.ID, types.ContainerLogsOptions{Since: sinceReq, ShowStdout: true, ShowStderr: true})
if err != nil {
panic(err)
}
b, err := ioutil.ReadAll(out)
if err != nil {
panic(err)
}
log.Printf("Rolling log Contener \n%s", string(b))
// Do something with the logs here...
}
}
}(nowUTC, exitCh)
select {
case err := <-errCh:
exitCh <- true
if err != nil {
panic(err)
}
case <-statusCh:
exitCh <- true
}