Skip to content

Commit caad727

Browse files
milasndeloof
authored andcommitted
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e. shows log output from every associated container). If a service is specified, e.g. `compose up foo`, then only `foo`'s logs are tailed. The `--attach-dependencies` flag can also be used, so that if `foo` depended upon `bar`, then `bar`'s logs would also be followed. It's also possible to use `--no-attach` to filter out one or more services explicitly, e.g. `compose up --no-attach=noisy` would launch all services, including `noisy`, and would show log output from every service _except_ `noisy`. Lastly, it's possible to use `up --attach` to explicitly restrict to a subset of services (or their dependencies). How these flags interact with each other is also worth thinking through. There were a few different connected issues here, but the primary issue was that running `compose up foo` was always attaching dependencies regardless of `--attach-dependencies`. The filtering logic here has been updated so that it behaves predictably both when launching all services (`compose up`) or a subset (`compose up foo`) as well as various flag combinations on top of those. Notably, this required making some changes to how it watches containers. The logic here between attaching for logs and monitoring for lifecycle changes is tightly coupled, so some changes were needed to ensure that the full set of services being `up`'d are _watched_ and the subset that should have logs shown are _attached_. (This does mean faking the attach with an event but not actually doing it.) While handling that, I adjusted the context lifetimes here, which improves error handling that gets shown to the user and should help avoid potential leaks by getting rid of a `context.Background()`. Signed-off-by: Milas Bowman <[email protected]>
1 parent 792afb8 commit caad727

File tree

6 files changed

+182
-53
lines changed

6 files changed

+182
-53
lines changed

cmd/compose/up.go

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package compose
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
23+
"strings"
2224
"time"
2325

2426
"github.com/compose-spec/compose-go/types"
@@ -83,7 +85,10 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
8385
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
8486
create.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
8587
if create.ignoreOrphans && create.removeOrphans {
86-
return fmt.Errorf("%s and --remove-orphans cannot be combined", ComposeIgnoreOrphans)
88+
return fmt.Errorf("cannot combine %s and --remove-orphans", ComposeIgnoreOrphans)
89+
}
90+
if len(up.attach) != 0 && up.attachDependencies {
91+
return errors.New("cannot combine --attach and --attach-dependencies")
8792
}
8893
return runUp(ctx, streams, backend, create, up, project, services)
8994
}),
@@ -108,12 +113,12 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
108113
flags.BoolVar(&up.noDeps, "no-deps", false, "Don't start linked services.")
109114
flags.BoolVar(&create.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")
110115
flags.BoolVarP(&create.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.")
111-
flags.BoolVar(&up.attachDependencies, "attach-dependencies", false, "Attach to dependent containers.")
112116
flags.BoolVar(&create.quietPull, "quiet-pull", false, "Pull without printing progress information.")
113-
flags.StringArrayVar(&up.attach, "attach", []string{}, "Attach to service output.")
114-
flags.StringArrayVar(&up.noAttach, "no-attach", []string{}, "Don't attach to specified service.")
117+
flags.StringArrayVar(&up.attach, "attach", []string{}, "Restrict attaching to the specified services. Incompatible with --attach-dependencies.")
118+
flags.StringArrayVar(&up.noAttach, "no-attach", []string{}, "Do not attach (stream logs) to the specified services.")
119+
flags.BoolVar(&up.attachDependencies, "attach-dependencies", false, "Automatically attach to log output of dependent services.")
115120
flags.BoolVar(&up.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.")
116-
flags.IntVar(&up.waitTimeout, "wait-timeout", 0, "timeout waiting for application to be running|healthy.")
121+
flags.IntVar(&up.waitTimeout, "wait-timeout", 0, "Maximum duration to wait for the project to be running|healthy.")
117122

118123
return upCmd
119124
}
@@ -158,37 +163,6 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
158163
return err
159164
}
160165

161-
var consumer api.LogConsumer
162-
if !upOptions.Detach {
163-
consumer = formatter.NewLogConsumer(ctx, streams.Out(), streams.Err(), !upOptions.noColor, !upOptions.noPrefix, upOptions.timestamp)
164-
}
165-
166-
attachTo := utils.Set[string]{}
167-
if len(upOptions.attach) > 0 {
168-
attachTo.AddAll(upOptions.attach...)
169-
}
170-
if upOptions.attachDependencies {
171-
if err := project.WithServices(attachTo.Elements(), func(s types.ServiceConfig) error {
172-
if s.Attach == nil || *s.Attach {
173-
attachTo.Add(s.Name)
174-
}
175-
return nil
176-
}); err != nil {
177-
return err
178-
}
179-
}
180-
if len(attachTo) == 0 {
181-
if err := project.WithServices(services, func(s types.ServiceConfig) error {
182-
if s.Attach == nil || *s.Attach {
183-
attachTo.Add(s.Name)
184-
}
185-
return nil
186-
}); err != nil {
187-
return err
188-
}
189-
}
190-
attachTo.RemoveAll(upOptions.noAttach...)
191-
192166
create := api.CreateOptions{
193167
Services: services,
194168
RemoveOrphans: createOptions.removeOrphans,
@@ -204,14 +178,48 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
204178
return backend.Create(ctx, project, create)
205179
}
206180

207-
timeout := time.Duration(upOptions.waitTimeout) * time.Second
181+
var consumer api.LogConsumer
182+
var attach []string
183+
if !upOptions.Detach {
184+
consumer = formatter.NewLogConsumer(ctx, streams.Out(), streams.Err(), !upOptions.noColor, !upOptions.noPrefix, upOptions.timestamp)
208185

186+
var attachSet utils.Set[string]
187+
if len(upOptions.attach) != 0 {
188+
// services are passed explicitly with --attach, verify they're valid and then use them as-is
189+
attachSet = utils.NewSet(upOptions.attach...)
190+
unexpectedSvcs := attachSet.Diff(utils.NewSet(project.ServiceNames()...))
191+
if len(unexpectedSvcs) != 0 {
192+
return fmt.Errorf("cannot attach to services not included in up: %s", strings.Join(unexpectedSvcs.Elements(), ", "))
193+
}
194+
} else {
195+
// mark services being launched (and potentially their deps) for attach
196+
// if they didn't opt-out via Compose YAML
197+
attachSet = utils.NewSet[string]()
198+
var dependencyOpt types.DependencyOption = types.IgnoreDependencies
199+
if upOptions.attachDependencies {
200+
dependencyOpt = types.IncludeDependencies
201+
}
202+
if err := project.WithServices(services, func(s types.ServiceConfig) error {
203+
if s.Attach == nil || *s.Attach {
204+
attachSet.Add(s.Name)
205+
}
206+
return nil
207+
}, dependencyOpt); err != nil {
208+
return err
209+
}
210+
}
211+
// filter out any services that have been explicitly marked for ignore with `--no-attach`
212+
attachSet.RemoveAll(upOptions.noAttach...)
213+
attach = attachSet.Elements()
214+
}
215+
216+
timeout := time.Duration(upOptions.waitTimeout) * time.Second
209217
return backend.Up(ctx, project, api.UpOptions{
210218
Create: create,
211219
Start: api.StartOptions{
212220
Project: project,
213221
Attach: consumer,
214-
AttachTo: attachTo.Elements(),
222+
AttachTo: attach,
215223
ExitCodeFrom: upOptions.exitCodeFrom,
216224
CascadeStop: upOptions.cascadeStop,
217225
Wait: upOptions.wait,

docs/reference/compose_up.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Create and start containers
99
|:-----------------------------|:--------------|:----------|:---------------------------------------------------------------------------------------------------------|
1010
| `--abort-on-container-exit` | | | Stops all containers if any container was stopped. Incompatible with -d |
1111
| `--always-recreate-deps` | | | Recreate dependent containers. Incompatible with --no-recreate. |
12-
| `--attach` | `stringArray` | | Attach to service output. |
13-
| `--attach-dependencies` | | | Attach to dependent containers. |
12+
| `--attach` | `stringArray` | | Restrict attaching to the specified services. Incompatible with --attach-dependencies. |
13+
| `--attach-dependencies` | | | Automatically attach to log output of dependent services. |
1414
| `--build` | | | Build images before starting containers. |
1515
| `-d`, `--detach` | | | Detached mode: Run containers in the background |
1616
| `--dry-run` | | | Execute command in dry run mode |
1717
| `--exit-code-from` | `string` | | Return the exit code of the selected service container. Implies --abort-on-container-exit |
1818
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
19-
| `--no-attach` | `stringArray` | | Don't attach to specified service. |
19+
| `--no-attach` | `stringArray` | | Do not attach (stream logs) to the specified services. |
2020
| `--no-build` | | | Don't build an image, even if it's missing. |
2121
| `--no-color` | | | Produce monochrome output. |
2222
| `--no-deps` | | | Don't start linked services. |
@@ -31,7 +31,7 @@ Create and start containers
3131
| `-t`, `--timeout` | `int` | `0` | Use this timeout in seconds for container shutdown when attached or when containers are already running. |
3232
| `--timestamps` | | | Show timestamps. |
3333
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
34-
| `--wait-timeout` | `int` | `0` | timeout waiting for application to be running\|healthy. |
34+
| `--wait-timeout` | `int` | `0` | Maximum duration to wait for the project to be running\|healthy. |
3535

3636

3737
<!---MARKER_GEN_END-->

docs/reference/docker_compose_up.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ options:
4848
- option: attach
4949
value_type: stringArray
5050
default_value: '[]'
51-
description: Attach to service output.
51+
description: |
52+
Restrict attaching to the specified services. Incompatible with --attach-dependencies.
5253
deprecated: false
5354
hidden: false
5455
experimental: false
@@ -58,7 +59,7 @@ options:
5859
- option: attach-dependencies
5960
value_type: bool
6061
default_value: "false"
61-
description: Attach to dependent containers.
62+
description: Automatically attach to log output of dependent services.
6263
deprecated: false
6364
hidden: false
6465
experimental: false
@@ -110,7 +111,7 @@ options:
110111
- option: no-attach
111112
value_type: stringArray
112113
default_value: '[]'
113-
description: Don't attach to specified service.
114+
description: Do not attach (stream logs) to the specified services.
114115
deprecated: false
115116
hidden: false
116117
experimental: false
@@ -266,7 +267,7 @@ options:
266267
- option: wait-timeout
267268
value_type: int
268269
default_value: "0"
269-
description: timeout waiting for application to be running|healthy.
270+
description: Maximum duration to wait for the project to be running|healthy.
270271
deprecated: false
271272
hidden: false
272273
experimental: false

pkg/compose/start.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,48 @@ func (s *composeService) start(ctx context.Context, projectName string, options
5656
}
5757
}
5858

59-
eg, ctx := errgroup.WithContext(ctx)
59+
// use an independent context tied to the errgroup for background attach operations
60+
// the primary context is still used for other operations
61+
// this means that once any attach operation fails, all other attaches are cancelled,
62+
// but an attach failing won't interfere with the rest of the start
63+
eg, attachCtx := errgroup.WithContext(ctx)
6064
if listener != nil {
61-
attached, err := s.attach(ctx, project, listener, options.AttachTo)
65+
_, err := s.attach(attachCtx, project, listener, options.AttachTo)
6266
if err != nil {
6367
return err
6468
}
6569

6670
eg.Go(func() error {
67-
return s.watchContainers(context.Background(), project.Name, options.AttachTo, options.Services, listener, attached,
71+
// it's possible to have a required service whose log output is not desired
72+
// (i.e. it's not in the attach set), so watch everything and then filter
73+
// calls to attach; this ensures that `watchContainers` blocks until all
74+
// required containers have exited, even if their output is not being shown
75+
attachTo := utils.NewSet[string](options.AttachTo...)
76+
required := utils.NewSet[string](options.Services...)
77+
toWatch := attachTo.Union(required).Elements()
78+
79+
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, toWatch...)
80+
if err != nil {
81+
return err
82+
}
83+
84+
// N.B. this uses the parent context (instead of attachCtx) so that the watch itself can
85+
// continue even if one of the log streams fails
86+
return s.watchContainers(ctx, project.Name, toWatch, required.Elements(), listener, containers,
6887
func(container moby.Container, _ time.Time) error {
69-
return s.attachContainer(ctx, container, listener)
88+
svc := container.Labels[api.ServiceLabel]
89+
if attachTo.Has(svc) {
90+
return s.attachContainer(attachCtx, container, listener)
91+
}
92+
93+
// HACK: simulate an "attach" event
94+
listener(api.ContainerEvent{
95+
Type: api.ContainerEventAttach,
96+
Container: getContainerNameWithoutProject(container),
97+
ID: container.ID,
98+
Service: svc,
99+
})
100+
return nil
70101
}, func(container moby.Container, _ time.Time) error {
71102
listener(api.ContainerEvent{
72103
Type: api.ContainerEventAttach,
@@ -156,6 +187,13 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
156187
required = services
157188
}
158189

190+
unexpected := utils.NewSet[string](required...).Diff(utils.NewSet[string](services...))
191+
if len(unexpected) != 0 {
192+
return fmt.Errorf(`required service(s) "%s" not present in watched service(s) "%s"`,
193+
strings.Join(unexpected.Elements(), ", "),
194+
strings.Join(services, ", "))
195+
}
196+
159197
// predicate to tell if a container we receive event for should be considered or ignored
160198
ofInterest := func(c moby.Container) bool {
161199
if len(services) > 0 {
@@ -190,6 +228,12 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
190228
err := s.Events(ctx, projectName, api.EventsOptions{
191229
Services: services,
192230
Consumer: func(event api.Event) error {
231+
defer func() {
232+
// after consuming each event, check to see if we're done
233+
if len(expected) == 0 {
234+
stop()
235+
}
236+
}()
193237
inspected, err := s.apiClient().ContainerInspect(ctx, event.Container)
194238
if err != nil {
195239
if errdefs.IsNotFound(err) {
@@ -291,9 +335,6 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
291335
}
292336
}
293337
}
294-
if len(expected) == 0 {
295-
stop()
296-
}
297338
return nil
298339
},
299340
})

pkg/utils/set.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ package utils
1616

1717
type Set[T comparable] map[T]struct{}
1818

19+
func NewSet[T comparable](v ...T) Set[T] {
20+
if len(v) == 0 {
21+
return make(Set[T])
22+
}
23+
24+
out := make(Set[T], len(v))
25+
for i := range v {
26+
out.Add(v[i])
27+
}
28+
return out
29+
}
30+
31+
func (s Set[T]) Has(v T) bool {
32+
_, ok := s[v]
33+
return ok
34+
}
35+
1936
func (s Set[T]) Add(v T) {
2037
s[v] = struct{}{}
2138
}
@@ -53,3 +70,24 @@ func (s Set[T]) RemoveAll(elements ...T) {
5370
s.Remove(e)
5471
}
5572
}
73+
74+
func (s Set[T]) Diff(other Set[T]) Set[T] {
75+
out := make(Set[T])
76+
for k := range s {
77+
if _, ok := other[k]; !ok {
78+
out[k] = struct{}{}
79+
}
80+
}
81+
return out
82+
}
83+
84+
func (s Set[T]) Union(other Set[T]) Set[T] {
85+
out := make(Set[T])
86+
for k := range s {
87+
out[k] = struct{}{}
88+
}
89+
for k := range other {
90+
out[k] = struct{}{}
91+
}
92+
return out
93+
}

pkg/utils/set_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2022 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
package utils
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func TestSet_Has(t *testing.T) {
24+
x := NewSet[string]("value")
25+
require.True(t, x.Has("value"))
26+
require.False(t, x.Has("VALUE"))
27+
}
28+
29+
func TestSet_Diff(t *testing.T) {
30+
a := NewSet[int](1, 2)
31+
b := NewSet[int](2, 3)
32+
require.ElementsMatch(t, []int{1}, a.Diff(b).Elements())
33+
require.ElementsMatch(t, []int{3}, b.Diff(a).Elements())
34+
}
35+
36+
func TestSet_Union(t *testing.T) {
37+
a := NewSet[int](1, 2)
38+
b := NewSet[int](2, 3)
39+
require.ElementsMatch(t, []int{1, 2, 3}, a.Union(b).Elements())
40+
require.ElementsMatch(t, []int{1, 2, 3}, b.Union(a).Elements())
41+
}

0 commit comments

Comments
 (0)