From c1c162eba696331fe2c44327d40cd2ede68efe0a Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 20 Dec 2022 14:53:04 +0100 Subject: [PATCH] don't fail `logs` when driver:none is set Signed-off-by: Nicolas De Loof --- pkg/compose/logs.go | 25 ++++++++++++++++++++++--- pkg/compose/start.go | 12 +++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/pkg/compose/logs.go b/pkg/compose/logs.go index e33c21c7756..7fdf15e3282 100644 --- a/pkg/compose/logs.go +++ b/pkg/compose/logs.go @@ -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" @@ -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 }) } @@ -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 diff --git a/pkg/compose/start.go b/pkg/compose/start.go index 9cc792cd4d5..b4a59b7137c 100644 --- a/pkg/compose/start.go +++ b/pkg/compose/start.go @@ -19,6 +19,7 @@ package compose import ( "context" "strings" + "time" "github.com/compose-spec/compose-go/types" "github.com/docker/compose/v2/pkg/utils" @@ -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) + }) }) } @@ -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 @@ -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 }