Merge pull request #1383 from ulyssessouza/attach-dependencies

Add --attach-dependencies
This commit is contained in:
Nicolas De loof 2021-03-03 16:22:09 +01:00 committed by GitHub
commit b6192f35f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 26 deletions

View File

@ -93,6 +93,8 @@ type CreateOptions struct {
type StartOptions struct { type StartOptions struct {
// Attach will attach to service containers and send container logs and events // Attach will attach to service containers and send container logs and events
Attach ContainerEventListener Attach ContainerEventListener
// Services passed in the command line to be started
Services []string
} }
// StopOptions group options of the Stop API // StopOptions group options of the Stop API

View File

@ -51,22 +51,23 @@ type composeOptions struct {
type upOptions struct { type upOptions struct {
*composeOptions *composeOptions
Detach bool Detach bool
Environment []string Environment []string
removeOrphans bool removeOrphans bool
forceRecreate bool forceRecreate bool
noRecreate bool noRecreate bool
recreateDeps bool recreateDeps bool
noStart bool noStart bool
noDeps bool noDeps bool
cascadeStop bool cascadeStop bool
exitCodeFrom string exitCodeFrom string
scale []string scale []string
noColor bool noColor bool
noPrefix bool noPrefix bool
timeChanged bool timeChanged bool
timeout int timeout int
noInherit bool noInherit bool
attachDependencies bool
} }
func (opts upOptions) recreateStrategy() string { func (opts upOptions) recreateStrategy() string {
@ -156,8 +157,8 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
if opts.Build && opts.noBuild { if opts.Build && opts.noBuild {
return fmt.Errorf("--build and --no-build are incompatible") return fmt.Errorf("--build and --no-build are incompatible")
} }
if opts.cascadeStop && opts.Detach { if opts.Detach && (opts.attachDependencies || opts.cascadeStop) {
return fmt.Errorf("--abort-on-container-exit and --detach are incompatible") return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies")
} }
if opts.forceRecreate && opts.noRecreate { if opts.forceRecreate && opts.noRecreate {
return fmt.Errorf("--force-recreate and --no-recreate are incompatible") return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
@ -194,6 +195,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.") flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.")
flags.BoolVar(&opts.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.") flags.BoolVar(&opts.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")
flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.") flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.")
flags.BoolVar(&opts.attachDependencies, "attach-dependencies", false, "Attach to dependent containers.")
} }
return upCmd return upCmd
@ -254,6 +256,10 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
return nil return nil
} }
if opts.attachDependencies {
services = nil
}
if opts.Detach { if opts.Detach {
return nil return nil
} }
@ -295,6 +301,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
Attach: func(event compose.ContainerEvent) { Attach: func(event compose.ContainerEvent) {
queue <- event queue <- event
}, },
Services: services,
}) })
if err != nil { if err != nil {
return err return err

View File

@ -31,8 +31,8 @@ import (
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
) )
func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener) (Containers, error) { func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener, selectedServices []string) (Containers, error) {
containers, err := s.getContainers(ctx, project, oneOffExclude) containers, err := s.getContainers(ctx, project, oneOffExclude, selectedServices)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -37,7 +37,7 @@ const (
oneOffOnly oneOffOnly
) )
func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff) (Containers, error) { func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff, selectedServices []string) (Containers, error) {
var containers Containers var containers Containers
f := filters.NewArgs( f := filters.NewArgs(
projectFilter(project.Name), projectFilter(project.Name),
@ -56,7 +56,10 @@ func (s *composeService) getContainers(ctx context.Context, project *types.Proje
if err != nil { if err != nil {
return nil, err return nil, err
} }
containers = containers.filter(isService(project.ServiceNames()...)) if len(selectedServices) == 0 {
selectedServices = project.ServiceNames()
}
containers = containers.filter(isService(selectedServices...))
return containers, nil return containers, nil
} }

View File

@ -27,7 +27,7 @@ import (
) )
func (s *composeService) Pause(ctx context.Context, project *types.Project) error { func (s *composeService) Pause(ctx context.Context, project *types.Project) error {
containers, err := s.getContainers(ctx, project, oneOffExclude) containers, err := s.getContainers(ctx, project, oneOffExclude, nil)
if err != nil { if err != nil {
return err return err
} }
@ -49,7 +49,7 @@ func (s *composeService) Pause(ctx context.Context, project *types.Project) erro
} }
func (s *composeService) UnPause(ctx context.Context, project *types.Project) error { func (s *composeService) UnPause(ctx context.Context, project *types.Project) error {
containers, err := s.getContainers(ctx, project, oneOffExclude) containers, err := s.getContainers(ctx, project, oneOffExclude, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -29,7 +29,7 @@ import (
) )
func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) { func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
containers, err := s.getContainers(ctx, project, oneOffInclude) containers, err := s.getContainers(ctx, project, oneOffInclude, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -29,7 +29,7 @@ import (
func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error { func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
var containers Containers var containers Containers
if options.Attach != nil { if options.Attach != nil {
c, err := s.attach(ctx, project, options.Attach) c, err := s.attach(ctx, project, options.Attach, options.Services)
if err != nil { if err != nil {
return err return err
} }