From 27d67773768222ffc57f124b38c767f36a575f96 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 26 Jun 2013 12:50:20 -0700 Subject: [PATCH 1/2] Display containers logs in case of build failure --- buildfile.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/buildfile.go b/buildfile.go index 9cbaac4e7..355a99c6c 100644 --- a/buildfile.go +++ b/buildfile.go @@ -29,6 +29,7 @@ type buildFile struct { config *Config context string + lastContainer *Container tmpContainers map[string]struct{} tmpImages map[string]struct{} @@ -225,6 +226,7 @@ func (b *buildFile) CmdAdd(args string) error { return err } b.tmpContainers[container.ID] = struct{}{} + b.lastContainer = container if err := container.EnsureMounted(); err != nil { return err @@ -260,6 +262,7 @@ func (b *buildFile) run() (string, error) { return "", err } b.tmpContainers[c.ID] = struct{}{} + b.lastContainer = c fmt.Fprintf(b.out, " ---> Running in %s\n", utils.TruncateID(c.ID)) //start the container @@ -301,6 +304,7 @@ func (b *buildFile) commit(id string, autoCmd []string, comment string) error { return err } b.tmpContainers[container.ID] = struct{}{} + b.lastContainer = container fmt.Fprintf(b.out, " ---> Running in %s\n", utils.TruncateID(container.ID)) id = container.ID if err := container.EnsureMounted(); err != nil { @@ -328,6 +332,29 @@ func (b *buildFile) commit(id string, autoCmd []string, comment string) error { } func (b *buildFile) Build(context io.Reader) (string, error) { + defer func() { + // If we have an error and a container, the display the logs + if b.lastContainer != nil { + fmt.Fprintf(b.out, "Logs from last container (%s):\n", b.lastContainer.ShortID()) + + cLog, err := b.lastContainer.ReadLog("stdout") + if err != nil { + utils.Debugf("Error reading logs (stdout): %s", err) + } + if _, err := io.Copy(b.out, cLog); err != nil { + utils.Debugf("Error streaming logs (stdout): %s", err) + } + cLog, err = b.lastContainer.ReadLog("stderr") + if err != nil { + utils.Debugf("Error reading logs (stderr): %s", err) + } + if _, err := io.Copy(b.out, cLog); err != nil { + utils.Debugf("Error streaming logs (stderr): %s", err) + } + fmt.Fprintf(b.out, "End of logs for %s\n", b.lastContainer.ShortID()) + } + }() + // FIXME: @creack any reason for using /tmp instead of ""? // FIXME: @creack "name" is a terrible variable name name, err := ioutil.TempDir("/tmp", "docker-build") @@ -380,6 +407,7 @@ func (b *buildFile) Build(context io.Reader) (string, error) { return "", ret.(error) } + b.lastContainer = nil fmt.Fprintf(b.out, " ---> %v\n", utils.TruncateID(b.image)) } if b.image != "" { From 2a20e852032339b4c91a01748119701c7ada234d Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Thu, 27 Jun 2013 11:10:19 -0700 Subject: [PATCH 2/2] Improve last log output --- buildfile.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buildfile.go b/buildfile.go index 355a99c6c..e826203c8 100644 --- a/buildfile.go +++ b/buildfile.go @@ -335,7 +335,7 @@ func (b *buildFile) Build(context io.Reader) (string, error) { defer func() { // If we have an error and a container, the display the logs if b.lastContainer != nil { - fmt.Fprintf(b.out, "Logs from last container (%s):\n", b.lastContainer.ShortID()) + fmt.Fprintf(b.out, "******** Logs from last container (%s) *******\n", b.lastContainer.ShortID()) cLog, err := b.lastContainer.ReadLog("stdout") if err != nil { @@ -351,7 +351,7 @@ func (b *buildFile) Build(context io.Reader) (string, error) { if _, err := io.Copy(b.out, cLog); err != nil { utils.Debugf("Error streaming logs (stderr): %s", err) } - fmt.Fprintf(b.out, "End of logs for %s\n", b.lastContainer.ShortID()) + fmt.Fprintf(b.out, "************* End of logs for %s *************\n", b.lastContainer.ShortID()) } }()