|
|
@@ -20,12 +20,13 @@ import (
|
|
|
"context"
|
|
|
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
+ convert "github.com/docker/compose-cli/local/moby"
|
|
|
"github.com/docker/compose-cli/utils"
|
|
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
moby "github.com/docker/docker/api/types"
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
- "github.com/sirupsen/logrus"
|
|
|
+ "golang.org/x/sync/errgroup"
|
|
|
)
|
|
|
|
|
|
func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
|
|
|
@@ -35,11 +36,52 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
|
|
|
|
|
|
var containers Containers
|
|
|
if options.Attach != nil {
|
|
|
- c, err := s.attach(ctx, project, options.Attach, options.Services)
|
|
|
+ attached, err := s.attach(ctx, project, options.Attach, options.Services)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- containers = c
|
|
|
+ containers = attached
|
|
|
+
|
|
|
+ // Watch events to capture container restart and re-attach
|
|
|
+ go func() {
|
|
|
+ watched := map[string]struct{}{}
|
|
|
+ for _, c := range containers {
|
|
|
+ watched[c.ID] = struct{}{}
|
|
|
+ }
|
|
|
+ s.Events(ctx, project.Name, compose.EventsOptions{ // nolint: errcheck
|
|
|
+ Services: options.Services,
|
|
|
+ Consumer: func(event compose.Event) error {
|
|
|
+ if event.Status == "start" {
|
|
|
+ inspect, err := s.apiClient.ContainerInspect(ctx, event.Container)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ container := moby.Container{
|
|
|
+ ID: event.Container,
|
|
|
+ Names: []string{inspect.Name},
|
|
|
+ State: convert.ContainerRunning,
|
|
|
+ Labels: map[string]string{
|
|
|
+ projectLabel: project.Name,
|
|
|
+ serviceLabel: event.Service,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ // Just ignore errors when reattaching to already crashed containers
|
|
|
+ s.attachContainer(ctx, container, options.Attach, project) // nolint: errcheck
|
|
|
+
|
|
|
+ if _, ok := watched[inspect.ID]; !ok {
|
|
|
+ // a container has been added to the service, see --scale option
|
|
|
+ watched[inspect.ID] = struct{}{}
|
|
|
+ go func() {
|
|
|
+ s.waitContainer(container, options.Attach) // nolint: errcheck
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }()
|
|
|
}
|
|
|
|
|
|
err := InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
|
|
@@ -56,16 +98,17 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+ eg, ctx := errgroup.WithContext(ctx)
|
|
|
for _, c := range containers {
|
|
|
c := c
|
|
|
- go func() {
|
|
|
- s.waitContainer(c, options.Attach)
|
|
|
- }()
|
|
|
+ eg.Go(func() error {
|
|
|
+ return s.waitContainer(c, options.Attach)
|
|
|
+ })
|
|
|
}
|
|
|
- return nil
|
|
|
+ return eg.Wait()
|
|
|
}
|
|
|
|
|
|
-func (s *composeService) waitContainer(c moby.Container, listener compose.ContainerEventListener) {
|
|
|
+func (s *composeService) waitContainer(c moby.Container, listener compose.ContainerEventListener) error {
|
|
|
statusC, errC := s.apiClient.ContainerWait(context.Background(), c.ID, container.WaitConditionNotRunning)
|
|
|
name := getContainerNameWithoutProject(c)
|
|
|
select {
|
|
|
@@ -76,7 +119,8 @@ func (s *composeService) waitContainer(c moby.Container, listener compose.Contai
|
|
|
Service: c.Labels[serviceLabel],
|
|
|
ExitCode: int(status.StatusCode),
|
|
|
})
|
|
|
+ return nil
|
|
|
case err := <-errC:
|
|
|
- logrus.Warnf("Unexpected API error for %s : %s", name, err.Error())
|
|
|
+ return err
|
|
|
}
|
|
|
}
|