Fix -f logs follow with stopped container

Fix -f logs follow with stopped container. Close #6531

Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
Qi Wang 2020-06-10 14:14:01 -04:00
parent 1f05606fac
commit 033743cbee
2 changed files with 27 additions and 0 deletions

View File

@ -30,6 +30,13 @@ func (c *Container) ReadLog(options *logs.LogOptions, logChannel chan *logs.LogL
}
func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *logs.LogLine) error {
state, err := c.State()
if err != nil {
return err
}
if state != define.ContainerStateRunning && state != define.ContainerStatePaused {
options.Follow = false
}
t, tailLog, err := logs.GetLogFile(c.LogPath(), options)
if err != nil {
// If the log file does not exist, this is not fatal.
@ -69,6 +76,14 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l
if nll.Since(options.Since) {
logChannel <- nll
}
state, err := c.State()
if err != nil {
logrus.Error(err)
break
}
if options.Follow && state != define.ContainerStateRunning && state != define.ContainerStatePaused {
t.Kill(err)
}
}
options.WaitGroup.Done()
}()

View File

@ -288,4 +288,16 @@ var _ = Describe("Podman logs", func() {
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))
})
It("follow output stopped container", func() {
containerName := "logs-f"
logc := podmanTest.Podman([]string{"run", "--name", containerName, "-d", ALPINE})
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))
results := podmanTest.Podman([]string{"logs", "-f", containerName})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
})
})