diff --git a/cmd/compose/create.go b/cmd/compose/create.go index 266f6fc1..7ce8a185 100644 --- a/cmd/compose/create.go +++ b/cmd/compose/create.go @@ -31,6 +31,7 @@ type createOptions struct { Build bool noBuild bool removeOrphans bool + ignoreOrphans bool forceRecreate bool noRecreate bool recreateDeps bool @@ -57,6 +58,7 @@ func createCommand(p *projectOptions, backend api.Service) *cobra.Command { RunE: p.WithProject(func(ctx context.Context, project *types.Project) error { return backend.Create(ctx, project, api.CreateOptions{ RemoveOrphans: opts.removeOrphans, + IgnoreOrphans: opts.ignoreOrphans, Recreate: opts.recreateStrategy(), RecreateDependencies: opts.dependenciesRecreateStrategy(), Inherit: !opts.noInherit, diff --git a/cmd/compose/up.go b/cmd/compose/up.go index 70460d64..cfde2178 100644 --- a/cmd/compose/up.go +++ b/cmd/compose/up.go @@ -120,6 +120,11 @@ func upCommand(p *projectOptions, backend api.Service) *cobra.Command { return nil }), RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error { + ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"] + create.ignoreOrphans = strings.ToLower(ignore) == "true" + if create.ignoreOrphans && create.removeOrphans { + return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined") + } return runUp(ctx, backend, create, up, project, services) }), ValidArgsFunction: serviceCompletion(p), @@ -177,6 +182,7 @@ func runUp(ctx context.Context, backend api.Service, createOptions createOptions create := api.CreateOptions{ Services: services, RemoveOrphans: createOptions.removeOrphans, + IgnoreOrphans: createOptions.ignoreOrphans, Recreate: createOptions.recreateStrategy(), RecreateDependencies: createOptions.dependenciesRecreateStrategy(), Inherit: !createOptions.noInherit, diff --git a/pkg/api/api.go b/pkg/api/api.go index 992997b8..9efaa270 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -100,6 +100,8 @@ type CreateOptions struct { Services []string // Remove legacy containers for services that are not defined in the project RemoveOrphans bool + // Ignore legacy containers for services that are not defined in the project + IgnoreOrphans bool // Recreate define the strategy to apply on existing containers Recreate string // RecreateDependencies define the strategy to apply on dependencies services diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 6698ae7c..4facd02d 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -89,7 +89,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt allServiceNames = append(allServiceNames, service.Name) } orphans := observedState.filter(isNotService(allServiceNames...)) - if len(orphans) > 0 { + if len(orphans) > 0 && !options.IgnoreOrphans { if options.RemoveOrphans { w := progress.ContextWriter(ctx) err := s.removeContainers(ctx, w, orphans, nil, false)