Skip to content

Commit

Permalink
don't fail logs when driver:none is set
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Dec 20, 2022
1 parent 9f5f0b6 commit c1c162e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
25 changes: 22 additions & 3 deletions pkg/compose/logs.go
Expand Up @@ -20,9 +20,12 @@ import (
"context"
"io"
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/stdcopy"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -59,7 +62,12 @@ func (s *composeService) Logs(
for _, c := range containers {
c := c
eg.Go(func() error {
return s.logContainers(ctx, consumer, c, options)
err := s.logContainers(ctx, consumer, c, options)
if _, ok := err.(errdefs.ErrNotImplemented); ok {
logrus.Warnf("Can't retrieve logs for %q: %s", getCanonicalContainerName(c), err.Error())
return nil
}
return err
})
}

Expand All @@ -79,13 +87,24 @@ func (s *composeService) Logs(
}

eg.Go(func() error {
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container, t time.Time) error {
printer.HandleEvent(api.ContainerEvent{
Type: api.ContainerEventAttach,
Container: getContainerNameWithoutProject(c),
Service: c.Labels[api.ServiceLabel],
})
return s.logContainers(ctx, consumer, c, options)
err := s.logContainers(ctx, consumer, c, api.LogOptions{
Follow: options.Follow,
Since: t.Format(time.RFC3339Nano),
Until: options.Until,
Tail: options.Tail,
Timestamps: options.Timestamps,
})
if _, ok := err.(errdefs.ErrNotImplemented); ok {
// ignore
return nil
}
return err
})
printer.Stop()
return err
Expand Down
12 changes: 7 additions & 5 deletions pkg/compose/start.go
Expand Up @@ -19,6 +19,7 @@ package compose
import (
"context"
"strings"
"time"

"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/utils"
Expand Down Expand Up @@ -59,9 +60,10 @@ func (s *composeService) start(ctx context.Context, projectName string, options
}

eg.Go(func() error {
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached, func(container moby.Container) error {
return s.attachContainer(ctx, container, listener)
})
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached,
func(container moby.Container, _ time.Time) error {
return s.attachContainer(ctx, container, listener)
})
})
}

Expand Down Expand Up @@ -107,7 +109,7 @@ func getDependencyCondition(service types.ServiceConfig, project *types.Project)
return ServiceConditionRunningOrHealthy
}

type containerWatchFn func(container moby.Container) error
type containerWatchFn func(container moby.Container, t time.Time) error

// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
Expand Down Expand Up @@ -193,7 +195,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
}
if mustAttach {
// Container restarted, need to re-attach
err := onStart(container)
err := onStart(container, event.Timestamp)
if err != nil {
return err
}
Expand Down

0 comments on commit c1c162e

Please sign in to comment.