Skip to content

Commit 6b9dcae

Browse files
committed
Improve output capturing for Docker build cmd
1 parent 6aa9930 commit 6b9dcae

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

client/jobfile/reader.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jobfile
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"os"
78
"os/exec"
89
"path"
@@ -201,11 +202,25 @@ func buildImage(dockerfile string, dir string, buildOptions []string, readOption
201202
cmd := exec.Command("docker", args...)
202203

203204
cmd.Dir = dir
204-
cmd.Stdout = lo.Ternary(readOptions.Verbose, os.Stdout, nil)
205-
cmd.Stderr = lo.Ternary(readOptions.Verbose, os.Stderr, nil)
205+
206+
// 1. Create a single buffer to capture the combined output (context + error)
207+
var combinedOutput bytes.Buffer
208+
209+
// 2. Configure streams based on verbosity
210+
if readOptions.Verbose {
211+
// Print to terminal in real-time AND write to our buffer
212+
cmd.Stdout = io.MultiWriter(os.Stdout, &combinedOutput)
213+
cmd.Stderr = io.MultiWriter(os.Stderr, &combinedOutput)
214+
} else {
215+
// Quiet mode: Write ONLY to our buffer
216+
cmd.Stdout = &combinedOutput
217+
cmd.Stderr = &combinedOutput
218+
}
206219

207220
if err := cmd.Run(); err != nil {
208-
return "", fmt.Errorf("failed to build image: %w", err)
221+
// 3. Clean up the output and attach it to the returned error
222+
outputStr := strings.TrimSpace(combinedOutput.String())
223+
return "", fmt.Errorf("failed to build image: %w\n\n--- Docker Build Log ---\n%s", err, outputStr)
209224
}
210225

211226
imageId, err := os.ReadFile(tmp.Name())

0 commit comments

Comments
 (0)