Skip to content

Commit 22d2e83

Browse files
committed
don't fail logs when driver:none is set
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 9f5f0b6 commit 22d2e83

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

pkg/compose/logs.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import (
2020
"context"
2121
"io"
2222
"strings"
23+
"time"
2324

2425
"github.com/docker/docker/api/types"
26+
"github.com/docker/docker/errdefs"
2527
"github.com/docker/docker/pkg/stdcopy"
28+
"github.com/sirupsen/logrus"
2629
"golang.org/x/sync/errgroup"
2730

2831
"github.com/docker/compose/v2/pkg/api"
@@ -59,7 +62,12 @@ func (s *composeService) Logs(
5962
for _, c := range containers {
6063
c := c
6164
eg.Go(func() error {
62-
return s.logContainers(ctx, consumer, c, options)
65+
err := s.logContainers(ctx, consumer, c, options)
66+
if _, ok := err.(errdefs.ErrNotImplemented); ok {
67+
logrus.Warnf("Can't retrieve logs for %q: %s", getCanonicalContainerName(c), err.Error())
68+
return nil
69+
}
70+
return err
6371
})
6472
}
6573

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

8189
eg.Go(func() error {
82-
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
90+
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container, t time.Time) error {
8391
printer.HandleEvent(api.ContainerEvent{
8492
Type: api.ContainerEventAttach,
8593
Container: getContainerNameWithoutProject(c),
8694
Service: c.Labels[api.ServiceLabel],
8795
})
88-
return s.logContainers(ctx, consumer, c, options)
96+
err := s.logContainers(ctx, consumer, c, api.LogOptions{
97+
Follow: options.Follow,
98+
Since: t.Format(time.RFC3339Nano),
99+
Until: options.Until,
100+
Tail: options.Tail,
101+
Timestamps: options.Timestamps,
102+
})
103+
if _, ok := err.(errdefs.ErrNotImplemented); ok {
104+
// ignore
105+
return nil
106+
}
107+
return err
89108
})
90109
printer.Stop()
91110
return err

pkg/compose/start.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"context"
2121
"strings"
22+
"time"
2223

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

6162
eg.Go(func() error {
62-
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached, func(container moby.Container) error {
63-
return s.attachContainer(ctx, container, listener)
64-
})
63+
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached,
64+
func(container moby.Container, _ time.Time) error {
65+
return s.attachContainer(ctx, container, listener)
66+
})
6567
})
6668
}
6769

@@ -107,7 +109,7 @@ func getDependencyCondition(service types.ServiceConfig, project *types.Project)
107109
return ServiceConditionRunningOrHealthy
108110
}
109111

110-
type containerWatchFn func(container moby.Container) error
112+
type containerWatchFn func(container moby.Container, t time.Time) error
111113

112114
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
113115
func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
@@ -167,7 +169,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
167169
restarted := watched[container.ID]
168170
watched[container.ID] = restarted + 1
169171
// Container terminated.
170-
willRestart := willContainerRestart(inspected, restarted)
172+
willRestart := inspected.State.Restarting
171173

172174
listener(api.ContainerEvent{
173175
Type: api.ContainerEventExit,
@@ -193,7 +195,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
193195
}
194196
if mustAttach {
195197
// Container restarted, need to re-attach
196-
err := onStart(container)
198+
err := onStart(container, event.Timestamp)
197199
if err != nil {
198200
return err
199201
}
@@ -210,14 +212,3 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
210212
}
211213
return err
212214
}
213-
214-
func willContainerRestart(container moby.ContainerJSON, restarted int) bool {
215-
policy := container.HostConfig.RestartPolicy
216-
if policy.IsAlways() || policy.IsUnlessStopped() {
217-
return true
218-
}
219-
if policy.IsOnFailure() {
220-
return container.State.ExitCode != 0 && policy.MaximumRetryCount > restarted
221-
}
222-
return false
223-
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
ping:
3+
image: alpine
4+
command: "sh -c 'ping -c 1 localhost && exit 1'"
5+
restart: "on-failure:2"

pkg/e2e/logs_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,22 @@ func TestLocalComposeLogs(t *testing.T) {
5656
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
5757
})
5858
}
59+
60+
func TestLocalComposeLogsFollow(t *testing.T) {
61+
c := NewParallelCLI(t)
62+
63+
const projectName = "compose-e2e-logs-restart"
64+
65+
t.Run("up", func(t *testing.T) {
66+
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/restart.yaml", "--project-name", projectName, "up", "-d")
67+
})
68+
69+
t.Run("logs", func(t *testing.T) {
70+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "logs", "--follow")
71+
assert.Check(t, strings.Count(res.Combined(), "PING localhost (127.0.0.1)") == 2)
72+
})
73+
74+
t.Run("down", func(t *testing.T) {
75+
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
76+
})
77+
}

0 commit comments

Comments
 (0)