Skip to content

Commit

Permalink
[shim] Fix container logs processing
Browse files Browse the repository at this point in the history
As the shim creates the container with Tty: false (the default),
Client.ContainerLogs returns the logs in the the multiplexed format,
which requires additional processing (demultiplexing).

See ContainerLogs documentation for details.

Fixes: #1720
  • Loading branch information
un-def committed Sep 24, 2024
1 parent fe272d3 commit 397fdb6
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions runner/internal/shim/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
docker "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
"github.com/dstackai/dstack/runner/consts"
"github.com/dstackai/dstack/runner/internal/shim/backends"
Expand Down Expand Up @@ -626,14 +627,20 @@ func getContainerLastLogs(client docker.APIClient, containerID string, n int) ([
}

ctx := context.Background()
reader, err := client.ContainerLogs(ctx, containerID, options)
muxedReader, err := client.ContainerLogs(ctx, containerID, options)
if err != nil {
return nil, err
}
defer reader.Close()
defer muxedReader.Close()

demuxedBuffer := new(bytes.Buffer)
// Using the same Writer for both stdout and stderr should be roughly equivalent to 2>&1
if _, err := stdcopy.StdCopy(demuxedBuffer, demuxedBuffer, muxedReader); err != nil {
return nil, err
}

var lines []string
scanner := bufio.NewScanner(reader)
scanner := bufio.NewScanner(demuxedBuffer)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
Expand Down

0 comments on commit 397fdb6

Please sign in to comment.