Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
flush container logs on error (when not attached)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bogdanov committed Aug 18, 2015
1 parent 6ef88f9 commit 8f28836
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/compose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func (client *ClientCfg) StartContainer(container *Container) error {
// TODO: HostConfig may be changed without re-creation of containers
// so of Volumes or Links are changed, we just need to restart container
if err := client.Docker.StartContainer(container.Id, container.Config.GetApiHostConfig()); err != nil {
if !client.Attach {
client.flushContainerLogs(container)
}
return fmt.Errorf("Failed to start container, error: %s", err)
}

Expand All @@ -230,22 +233,9 @@ func (client *ClientCfg) StartContainer(container *Container) error {
time.Sleep(client.Wait)

if err := client.EnsureContainerState(container); err != nil {
// TODO: create container io once in some place?
if !client.Attach {
container.Io = NewContainerIo(container)

err2 := client.Docker.Logs(docker.LogsOptions{
Container: container.Name.String(),
OutputStream: container.Io.Stdout,
ErrorStream: container.Io.Stderr,
Stdout: true,
Stderr: true,
})
if err2 != nil {
log.Errorf("Failed to read logs of container %s, error: %s", container.Name, err2)
}
client.flushContainerLogs(container)
}

return err
}
}
Expand Down Expand Up @@ -641,3 +631,20 @@ func (client *ClientCfg) listenReAttach(containers []*Container) {
}
}
}

func (client *ClientCfg) flushContainerLogs(container *Container) {
if container.Io == nil {
container.Io = NewContainerIo(container)
}

err2 := client.Docker.Logs(docker.LogsOptions{
Container: container.Name.String(),
OutputStream: container.Io.Stdout,
ErrorStream: container.Io.Stderr,
Stdout: true,
Stderr: true,
})
if err2 != nil {
log.Errorf("Failed to read logs of container %s, error: %s", container.Name, err2)
}
}

0 comments on commit 8f28836

Please sign in to comment.