diff --git a/pkg/compose/logs.go b/pkg/compose/logs.go index e33c21c775..7fdf15e328 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 9cc792cd4d..e2c7bc263e 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 @@ -167,7 +169,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo restarted := watched[container.ID] watched[container.ID] = restarted + 1 // Container terminated. - willRestart := willContainerRestart(inspected, restarted) + willRestart := inspected.State.Restarting listener(api.ContainerEvent{ Type: api.ContainerEventExit, @@ -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 } @@ -210,14 +212,3 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo } return err } - -func willContainerRestart(container moby.ContainerJSON, restarted int) bool { - policy := container.HostConfig.RestartPolicy - if policy.IsAlways() || policy.IsUnlessStopped() { - return true - } - if policy.IsOnFailure() { - return container.State.ExitCode != 0 && policy.MaximumRetryCount > restarted - } - return false -} diff --git a/pkg/e2e/fixtures/logs-test/restart.yaml b/pkg/e2e/fixtures/logs-test/restart.yaml new file mode 100644 index 0000000000..fcee219836 --- /dev/null +++ b/pkg/e2e/fixtures/logs-test/restart.yaml @@ -0,0 +1,5 @@ +services: + ping: + image: alpine + command: "sh -c 'ping -c 1 localhost && exit 1'" + restart: "on-failure:2" diff --git a/pkg/e2e/logs_test.go b/pkg/e2e/logs_test.go index 42d47d050f..68d06bd622 100644 --- a/pkg/e2e/logs_test.go +++ b/pkg/e2e/logs_test.go @@ -56,3 +56,22 @@ func TestLocalComposeLogs(t *testing.T) { _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down") }) } + +func TestLocalComposeLogsFollow(t *testing.T) { + c := NewParallelCLI(t) + + const projectName = "compose-e2e-logs-restart" + + t.Run("up", func(t *testing.T) { + c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/restart.yaml", "--project-name", projectName, "up", "-d") + }) + + t.Run("logs", func(t *testing.T) { + res := c.RunDockerComposeCmd(t, "--project-name", projectName, "logs", "--follow") + assert.Check(t, strings.Count(res.Combined(), "PING localhost (127.0.0.1)") == 2) + }) + + t.Run("down", func(t *testing.T) { + _ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down") + }) +}