mirror of https://github.com/docker/compose.git
don't fail `logs` when driver:none is set
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
9f5f0b6f03
commit
22d2e83896
|
@ -20,9 +20,12 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
"github.com/docker/docker/pkg/stdcopy"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
|
@ -59,7 +62,12 @@ func (s *composeService) Logs(
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
c := c
|
c := c
|
||||||
eg.Go(func() error {
|
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 {
|
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{
|
printer.HandleEvent(api.ContainerEvent{
|
||||||
Type: api.ContainerEventAttach,
|
Type: api.ContainerEventAttach,
|
||||||
Container: getContainerNameWithoutProject(c),
|
Container: getContainerNameWithoutProject(c),
|
||||||
Service: c.Labels[api.ServiceLabel],
|
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()
|
printer.Stop()
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -19,6 +19,7 @@ package compose
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
"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 {
|
eg.Go(func() error {
|
||||||
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached, func(container moby.Container) error {
|
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached,
|
||||||
return s.attachContainer(ctx, container, listener)
|
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
|
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
|
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
|
||||||
func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
|
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]
|
restarted := watched[container.ID]
|
||||||
watched[container.ID] = restarted + 1
|
watched[container.ID] = restarted + 1
|
||||||
// Container terminated.
|
// Container terminated.
|
||||||
willRestart := willContainerRestart(inspected, restarted)
|
willRestart := inspected.State.Restarting
|
||||||
|
|
||||||
listener(api.ContainerEvent{
|
listener(api.ContainerEvent{
|
||||||
Type: api.ContainerEventExit,
|
Type: api.ContainerEventExit,
|
||||||
|
@ -193,7 +195,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
|
||||||
}
|
}
|
||||||
if mustAttach {
|
if mustAttach {
|
||||||
// Container restarted, need to re-attach
|
// Container restarted, need to re-attach
|
||||||
err := onStart(container)
|
err := onStart(container, event.Timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -210,14 +212,3 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
|
||||||
}
|
}
|
||||||
return err
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
services:
|
||||||
|
ping:
|
||||||
|
image: alpine
|
||||||
|
command: "sh -c 'ping -c 1 localhost && exit 1'"
|
||||||
|
restart: "on-failure:2"
|
|
@ -56,3 +56,22 @@ func TestLocalComposeLogs(t *testing.T) {
|
||||||
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
_ = 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")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue