2020-11-17 10:15:07 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-08 11:53:36 +01:00
|
|
|
package compose
|
2020-11-17 10:15:07 +01:00
|
|
|
|
|
|
|
import (
|
2020-12-08 11:53:36 +01:00
|
|
|
"context"
|
2023-09-26 00:57:12 +02:00
|
|
|
"errors"
|
2023-02-13 17:42:11 +01:00
|
|
|
"fmt"
|
2025-05-27 14:00:21 +02:00
|
|
|
"slices"
|
2022-04-13 09:49:01 +02:00
|
|
|
"strings"
|
2022-12-20 14:53:04 +01:00
|
|
|
"time"
|
2020-11-18 17:18:41 +01:00
|
|
|
|
2025-05-29 10:37:19 +02:00
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
2023-01-26 16:44:40 +01:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/progress"
|
2022-12-07 14:43:07 +01:00
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2025-05-29 10:37:19 +02:00
|
|
|
containerType "github.com/docker/docker/api/types/container"
|
2023-01-26 16:44:40 +01:00
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2023-01-26 16:44:40 +01:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2021-05-25 13:33:13 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-11-17 10:15:07 +01:00
|
|
|
)
|
|
|
|
|
2022-02-05 10:27:52 +03:30
|
|
|
func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
|
2021-06-03 13:57:56 +02:00
|
|
|
return progress.Run(ctx, func(ctx context.Context) error {
|
2022-04-13 09:49:01 +02:00
|
|
|
return s.start(ctx, strings.ToLower(projectName), options, nil)
|
2023-05-11 18:45:00 +02:00
|
|
|
}, s.stdinfo())
|
2021-06-03 13:57:56 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 10:27:52 +03:30
|
|
|
func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
|
2022-04-07 12:13:30 +02:00
|
|
|
project := options.Project
|
|
|
|
if project == nil {
|
|
|
|
var containers Containers
|
|
|
|
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-20 08:09:14 +02:00
|
|
|
|
2022-04-07 12:13:30 +02:00
|
|
|
project, err = s.projectFromName(containers, projectName, options.AttachTo...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-24 23:46:07 +03:30
|
|
|
}
|
2022-02-05 10:27:52 +03:30
|
|
|
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
// use an independent context tied to the errgroup for background attach operations
|
|
|
|
// the primary context is still used for other operations
|
|
|
|
// this means that once any attach operation fails, all other attaches are cancelled,
|
|
|
|
// but an attach failing won't interfere with the rest of the start
|
|
|
|
eg, attachCtx := errgroup.WithContext(ctx)
|
2021-05-26 12:17:37 +02:00
|
|
|
if listener != nil {
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
_, err := s.attach(attachCtx, project, listener, options.AttachTo)
|
2020-12-08 11:53:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-25 13:33:13 +02:00
|
|
|
|
2021-05-26 12:17:37 +02:00
|
|
|
eg.Go(func() error {
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
// it's possible to have a required service whose log output is not desired
|
|
|
|
// (i.e. it's not in the attach set), so watch everything and then filter
|
|
|
|
// calls to attach; this ensures that `watchContainers` blocks until all
|
|
|
|
// required containers have exited, even if their output is not being shown
|
|
|
|
attachTo := utils.NewSet[string](options.AttachTo...)
|
|
|
|
required := utils.NewSet[string](options.Services...)
|
|
|
|
toWatch := attachTo.Union(required).Elements()
|
|
|
|
|
|
|
|
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, toWatch...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// N.B. this uses the parent context (instead of attachCtx) so that the watch itself can
|
|
|
|
// continue even if one of the log streams fails
|
|
|
|
return s.watchContainers(ctx, project.Name, toWatch, required.Elements(), listener, containers,
|
2025-02-12 09:34:07 +01:00
|
|
|
func(ctr containerType.Summary, _ time.Time) error {
|
|
|
|
svc := ctr.Labels[api.ServiceLabel]
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
if attachTo.Has(svc) {
|
2025-02-12 09:34:07 +01:00
|
|
|
return s.attachContainer(attachCtx, ctr, listener)
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: simulate an "attach" event
|
|
|
|
listener(api.ContainerEvent{
|
|
|
|
Type: api.ContainerEventAttach,
|
2025-02-12 09:34:07 +01:00
|
|
|
Container: getContainerNameWithoutProject(ctr),
|
|
|
|
ID: ctr.ID,
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
Service: svc,
|
|
|
|
})
|
|
|
|
return nil
|
2025-02-12 09:34:07 +01:00
|
|
|
}, func(ctr containerType.Summary, _ time.Time) error {
|
2023-02-03 14:36:39 +01:00
|
|
|
listener(api.ContainerEvent{
|
|
|
|
Type: api.ContainerEventAttach,
|
|
|
|
Container: "", // actual name will be set by start event
|
2025-02-12 09:34:07 +01:00
|
|
|
ID: ctr.ID,
|
|
|
|
Service: ctr.Labels[api.ServiceLabel],
|
2023-02-03 14:36:39 +01:00
|
|
|
})
|
|
|
|
return nil
|
2022-12-20 14:53:04 +01:00
|
|
|
})
|
2021-05-26 12:17:37 +02:00
|
|
|
})
|
2020-11-17 10:15:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 09:58:30 +01:00
|
|
|
var containers Containers
|
2023-12-05 10:59:26 +01:00
|
|
|
containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
|
2023-01-26 09:58:30 +01:00
|
|
|
Filters: filters.NewArgs(
|
|
|
|
projectFilter(project.Name),
|
|
|
|
oneOffFilter(false),
|
|
|
|
),
|
|
|
|
All: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = InDependencyOrder(ctx, project, func(c context.Context, name string) error {
|
2021-06-24 16:35:38 +02:00
|
|
|
service, err := project.GetService(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-11 17:52:31 +02:00
|
|
|
|
2024-09-26 15:14:42 +02:00
|
|
|
return s.startService(ctx, project, service, containers, listener, options.WaitTimeout)
|
2020-12-08 11:53:36 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-11 17:52:31 +02:00
|
|
|
|
|
|
|
if options.Wait {
|
|
|
|
depends := types.DependsOnConfig{}
|
|
|
|
for _, s := range project.Services {
|
|
|
|
depends[s.Name] = types.ServiceDependency{
|
2022-06-17 15:11:48 +02:00
|
|
|
Condition: getDependencyCondition(s, project),
|
2023-07-10 18:27:59 +02:00
|
|
|
Required: true,
|
2021-10-11 17:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-13 17:42:11 +01:00
|
|
|
if options.WaitTimeout > 0 {
|
|
|
|
withTimeout, cancel := context.WithTimeout(ctx, options.WaitTimeout)
|
|
|
|
ctx = withTimeout
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2024-09-23 16:05:53 +02:00
|
|
|
err = s.waitDependencies(ctx, project, project.Name, depends, containers, 0)
|
2021-10-11 17:52:31 +02:00
|
|
|
if err != nil {
|
2023-09-26 00:57:12 +02:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2023-02-13 17:42:11 +01:00
|
|
|
return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
|
|
|
|
}
|
2021-10-11 17:52:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 12:17:37 +02:00
|
|
|
return eg.Wait()
|
|
|
|
}
|
2021-02-08 11:04:46 +01:00
|
|
|
|
2022-06-17 15:11:48 +02:00
|
|
|
// getDependencyCondition checks if service is depended on by other services
|
|
|
|
// with service_completed_successfully condition, and applies that condition
|
|
|
|
// instead, or --wait will never finish waiting for one-shot containers
|
|
|
|
func getDependencyCondition(service types.ServiceConfig, project *types.Project) string {
|
|
|
|
for _, services := range project.Services {
|
|
|
|
for dependencyService, dependencyConfig := range services.DependsOn {
|
|
|
|
if dependencyService == service.Name && dependencyConfig.Condition == types.ServiceConditionCompletedSuccessfully {
|
|
|
|
return types.ServiceConditionCompletedSuccessfully
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ServiceConditionRunningOrHealthy
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:34:07 +01:00
|
|
|
type containerWatchFn func(ctr containerType.Summary, t time.Time) error
|
2021-06-09 10:05:49 +02:00
|
|
|
|
2021-05-26 12:17:37 +02:00
|
|
|
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
|
2022-12-08 18:53:47 +01:00
|
|
|
func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
|
|
|
|
projectName string, services, required []string,
|
2024-12-10 10:30:37 +01:00
|
|
|
listener api.ContainerEventListener, containers Containers, onStart, onRecreate containerWatchFn,
|
|
|
|
) error {
|
2022-12-08 18:53:47 +01:00
|
|
|
if len(containers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-07 14:43:07 +01:00
|
|
|
if len(required) == 0 {
|
|
|
|
required = services
|
|
|
|
}
|
|
|
|
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
unexpected := utils.NewSet[string](required...).Diff(utils.NewSet[string](services...))
|
|
|
|
if len(unexpected) != 0 {
|
|
|
|
return fmt.Errorf(`required service(s) "%s" not present in watched service(s) "%s"`,
|
|
|
|
strings.Join(unexpected.Elements(), ", "),
|
|
|
|
strings.Join(services, ", "))
|
|
|
|
}
|
|
|
|
|
2023-05-25 17:02:08 +02:00
|
|
|
// predicate to tell if a container we receive event for should be considered or ignored
|
2025-02-12 09:34:07 +01:00
|
|
|
ofInterest := func(c containerType.Summary) bool {
|
2023-05-25 17:02:08 +02:00
|
|
|
if len(services) > 0 {
|
|
|
|
// we only watch some services
|
2025-05-27 14:00:21 +02:00
|
|
|
return slices.Contains(services, c.Labels[api.ServiceLabel])
|
2023-05-25 17:02:08 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// predicate to tell if a container we receive event for should be watched until termination
|
2025-02-12 09:34:07 +01:00
|
|
|
isRequired := func(c containerType.Summary) bool {
|
2023-05-25 17:02:08 +02:00
|
|
|
if len(services) > 0 && len(required) > 0 {
|
|
|
|
// we only watch some services
|
2025-05-27 14:00:21 +02:00
|
|
|
return slices.Contains(required, c.Labels[api.ServiceLabel])
|
2023-05-25 17:02:08 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-12-07 14:43:07 +01:00
|
|
|
var (
|
2024-10-31 14:59:04 +00:00
|
|
|
expected = utils.NewSet[string]()
|
2022-12-07 14:43:07 +01:00
|
|
|
watched = map[string]int{}
|
2023-02-03 14:36:39 +01:00
|
|
|
replaced []string
|
2022-12-07 14:43:07 +01:00
|
|
|
)
|
2021-02-08 11:04:46 +01:00
|
|
|
for _, c := range containers {
|
2023-05-25 17:02:08 +02:00
|
|
|
if isRequired(c) {
|
2024-10-31 14:59:04 +00:00
|
|
|
expected.Add(c.ID)
|
2022-12-07 14:43:07 +01:00
|
|
|
}
|
2021-05-26 12:17:37 +02:00
|
|
|
watched[c.ID] = 0
|
2020-11-17 10:15:07 +01:00
|
|
|
}
|
2021-03-08 10:22:24 +01:00
|
|
|
|
2021-09-22 10:09:04 +02:00
|
|
|
ctx, stop := context.WithCancel(ctx)
|
2021-06-28 09:56:17 -03:00
|
|
|
err := s.Events(ctx, projectName, api.EventsOptions{
|
2021-05-26 12:17:37 +02:00
|
|
|
Services: services,
|
2021-06-14 16:26:14 +02:00
|
|
|
Consumer: func(event api.Event) error {
|
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 <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
|
|
|
defer func() {
|
|
|
|
// after consuming each event, check to see if we're done
|
|
|
|
if len(expected) == 0 {
|
|
|
|
stop()
|
|
|
|
}
|
|
|
|
}()
|
2022-02-23 11:28:56 +01:00
|
|
|
inspected, err := s.apiClient().ContainerInspect(ctx, event.Container)
|
2021-05-26 12:17:37 +02:00
|
|
|
if err != nil {
|
2025-05-29 10:37:19 +02:00
|
|
|
if cerrdefs.IsNotFound(err) {
|
2023-04-12 12:15:58 -04:00
|
|
|
// it's possible to get "destroy" or "kill" events but not
|
|
|
|
// be able to inspect in time before they're gone from the
|
|
|
|
// API, so just remove the watch without erroring
|
|
|
|
delete(watched, event.Container)
|
2024-10-31 14:59:04 +00:00
|
|
|
expected.Remove(event.Container)
|
2023-04-12 12:15:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 12:17:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2025-02-12 09:34:07 +01:00
|
|
|
container := containerType.Summary{
|
2021-05-26 12:17:37 +02:00
|
|
|
ID: inspected.ID,
|
|
|
|
Names: []string{inspected.Name},
|
|
|
|
Labels: inspected.Config.Labels,
|
|
|
|
}
|
|
|
|
name := getContainerNameWithoutProject(container)
|
2022-12-07 14:43:07 +01:00
|
|
|
service := container.Labels[api.ServiceLabel]
|
|
|
|
switch event.Status {
|
|
|
|
case "stop":
|
2024-07-24 13:34:01 +01:00
|
|
|
if inspected.State.Running {
|
|
|
|
// on sync+restart action the container stops -> dies -> start -> restart
|
|
|
|
// we do not want to stop the current container, we want to restart it
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-03 14:36:39 +01:00
|
|
|
if _, ok := watched[container.ID]; ok {
|
|
|
|
eType := api.ContainerEventStopped
|
2025-05-27 14:00:21 +02:00
|
|
|
if slices.Contains(replaced, container.ID) {
|
2025-05-27 14:27:45 +02:00
|
|
|
replaced = slices.DeleteFunc(replaced, func(e string) bool { return e == container.ID })
|
2023-02-03 14:36:39 +01:00
|
|
|
eType = api.ContainerEventRecreated
|
|
|
|
}
|
|
|
|
listener(api.ContainerEvent{
|
|
|
|
Type: eType,
|
|
|
|
Container: name,
|
|
|
|
ID: container.ID,
|
|
|
|
Service: service,
|
2024-04-16 15:44:23 +02:00
|
|
|
ExitCode: inspected.State.ExitCode,
|
2023-02-03 14:36:39 +01:00
|
|
|
})
|
|
|
|
}
|
2021-12-08 17:59:48 +01:00
|
|
|
|
|
|
|
delete(watched, container.ID)
|
2024-10-31 14:59:04 +00:00
|
|
|
expected.Remove(container.ID)
|
2022-12-07 14:43:07 +01:00
|
|
|
case "die":
|
2021-05-26 12:17:37 +02:00
|
|
|
restarted := watched[container.ID]
|
|
|
|
watched[container.ID] = restarted + 1
|
|
|
|
// Container terminated.
|
2022-12-20 14:53:04 +01:00
|
|
|
willRestart := inspected.State.Restarting
|
2024-07-24 13:34:01 +01:00
|
|
|
if inspected.State.Running {
|
|
|
|
// on sync+restart action inspected.State.Restarting is false,
|
|
|
|
// however the container is already running before it restarts
|
|
|
|
willRestart = true
|
|
|
|
}
|
2021-05-26 12:17:37 +02:00
|
|
|
|
2023-02-03 14:36:39 +01:00
|
|
|
eType := api.ContainerEventExit
|
2025-05-27 14:00:21 +02:00
|
|
|
if slices.Contains(replaced, container.ID) {
|
2025-05-27 14:27:45 +02:00
|
|
|
replaced = slices.DeleteFunc(replaced, func(e string) bool { return e == container.ID })
|
2023-02-03 14:36:39 +01:00
|
|
|
eType = api.ContainerEventRecreated
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
listener(api.ContainerEvent{
|
2023-02-03 14:36:39 +01:00
|
|
|
Type: eType,
|
2021-05-26 12:17:37 +02:00
|
|
|
Container: name,
|
2023-02-03 14:36:39 +01:00
|
|
|
ID: container.ID,
|
2022-12-07 14:43:07 +01:00
|
|
|
Service: service,
|
2021-05-26 12:17:37 +02:00
|
|
|
ExitCode: inspected.State.ExitCode,
|
|
|
|
Restarting: willRestart,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !willRestart {
|
|
|
|
// we're done with this one
|
|
|
|
delete(watched, container.ID)
|
2024-10-31 14:59:04 +00:00
|
|
|
expected.Remove(container.ID)
|
2021-05-26 12:17:37 +02:00
|
|
|
}
|
2022-12-07 14:43:07 +01:00
|
|
|
case "start":
|
2021-05-26 12:17:37 +02:00
|
|
|
count, ok := watched[container.ID]
|
|
|
|
mustAttach := ok && count > 0 // Container restarted, need to re-attach
|
|
|
|
if !ok {
|
|
|
|
// A new container has just been added to service by scale
|
|
|
|
watched[container.ID] = 0
|
2024-10-31 14:59:04 +00:00
|
|
|
expected.Add(container.ID)
|
2021-05-26 12:17:37 +02:00
|
|
|
mustAttach = true
|
|
|
|
}
|
|
|
|
if mustAttach {
|
|
|
|
// Container restarted, need to re-attach
|
2022-12-20 14:53:04 +01:00
|
|
|
err := onStart(container, event.Timestamp)
|
2021-05-26 12:17:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 14:36:39 +01:00
|
|
|
case "create":
|
|
|
|
if id, ok := container.Labels[api.ContainerReplaceLabel]; ok {
|
|
|
|
replaced = append(replaced, id)
|
|
|
|
err = onRecreate(container, event.Timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:59:04 +00:00
|
|
|
if expected.Has(id) {
|
|
|
|
expected.Add(inspected.ID)
|
|
|
|
expected.Add(container.ID)
|
2023-02-03 14:36:39 +01:00
|
|
|
}
|
|
|
|
watched[container.ID] = 1
|
2023-05-25 17:02:08 +02:00
|
|
|
} else if ofInterest(container) {
|
|
|
|
watched[container.ID] = 1
|
|
|
|
if isRequired(container) {
|
2024-10-31 14:59:04 +00:00
|
|
|
expected.Add(container.ID)
|
2023-05-25 17:02:08 +02:00
|
|
|
}
|
2023-02-03 14:36:39 +01:00
|
|
|
}
|
2021-05-26 12:17:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if errors.Is(ctx.Err(), context.Canceled) {
|
2021-05-25 13:33:13 +02:00
|
|
|
return nil
|
2021-03-08 10:22:24 +01:00
|
|
|
}
|
2021-05-26 12:17:37 +02:00
|
|
|
return err
|
2021-03-08 10:22:24 +01:00
|
|
|
}
|