mirror of
https://github.com/docker/compose.git
synced 2025-07-25 06:34:35 +02:00
Merge pull request #1190 from docker/recreate_flags
introduce --force-recreate and --no-recreate
This commit is contained in:
commit
c3443410fe
@ -57,6 +57,8 @@ type Service interface {
|
|||||||
type CreateOptions struct {
|
type CreateOptions struct {
|
||||||
// Remove legacy containers for services that are not defined in the project
|
// Remove legacy containers for services that are not defined in the project
|
||||||
RemoveOrphans bool
|
RemoveOrphans bool
|
||||||
|
// Recreate define the strategy to apply on existing containers
|
||||||
|
Recreate string
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpOptions group options of the Up API
|
// UpOptions group options of the Up API
|
||||||
@ -137,6 +139,15 @@ const (
|
|||||||
FAILED string = "Failed"
|
FAILED string = "Failed"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// RecreateDiverged to recreate services which configuration diverges from compose model
|
||||||
|
RecreateDiverged = "diverged"
|
||||||
|
// RecreateForce to force service container being recreated
|
||||||
|
RecreateForce = "force"
|
||||||
|
// RecreateNever to never recreate existing service containers
|
||||||
|
RecreateNever = "never"
|
||||||
|
)
|
||||||
|
|
||||||
// Stack holds the name and state of a compose application/stack
|
// Stack holds the name and state of a compose application/stack
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
ID string
|
ID string
|
||||||
|
@ -45,6 +45,18 @@ type upOptions struct {
|
|||||||
Detach bool
|
Detach bool
|
||||||
Environment []string
|
Environment []string
|
||||||
removeOrphans bool
|
removeOrphans bool
|
||||||
|
forceRecreate bool
|
||||||
|
noRecreate bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o upOptions) recreateStrategy() string {
|
||||||
|
if o.noRecreate {
|
||||||
|
return compose.RecreateNever
|
||||||
|
}
|
||||||
|
if o.forceRecreate {
|
||||||
|
return compose.RecreateForce
|
||||||
|
}
|
||||||
|
return compose.RecreateDiverged
|
||||||
}
|
}
|
||||||
|
|
||||||
func upCommand(p *projectOptions, contextType string) *cobra.Command {
|
func upCommand(p *projectOptions, contextType string) *cobra.Command {
|
||||||
@ -59,6 +71,9 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
switch contextType {
|
switch contextType {
|
||||||
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
|
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
|
||||||
|
if opts.forceRecreate && opts.noRecreate {
|
||||||
|
return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
|
||||||
|
}
|
||||||
return runCreateStart(cmd.Context(), opts, args)
|
return runCreateStart(cmd.Context(), opts, args)
|
||||||
default:
|
default:
|
||||||
return runUp(cmd.Context(), opts, args)
|
return runUp(cmd.Context(), opts, args)
|
||||||
@ -71,8 +86,13 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
|
|||||||
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
|
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
|
||||||
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
|
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
|
||||||
|
|
||||||
if contextType == store.AciContextType {
|
switch contextType {
|
||||||
|
case store.AciContextType:
|
||||||
flags.StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
|
flags.StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
|
||||||
|
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
|
||||||
|
flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
|
||||||
|
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return upCmd
|
return upCmd
|
||||||
@ -101,6 +121,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
|
|||||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
||||||
return "", c.ComposeService().Create(ctx, project, compose.CreateOptions{
|
return "", c.ComposeService().Create(ctx, project, compose.CreateOptions{
|
||||||
RemoveOrphans: opts.removeOrphans,
|
RemoveOrphans: opts.removeOrphans,
|
||||||
|
Recreate: opts.recreateStrategy(),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"github.com/docker/compose-cli/api/progress"
|
"github.com/docker/compose-cli/api/progress"
|
||||||
status "github.com/docker/compose-cli/local/moby"
|
status "github.com/docker/compose-cli/local/moby"
|
||||||
)
|
)
|
||||||
@ -74,7 +75,7 @@ func (s *composeService) ensureScale(ctx context.Context, actual []moby.Containe
|
|||||||
return eg, actual, nil
|
return eg, actual, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) ensureService(ctx context.Context, observedState Containers, project *types.Project, service types.ServiceConfig) error {
|
func (s *composeService) ensureService(ctx context.Context, observedState Containers, project *types.Project, service types.ServiceConfig, recreate string) error {
|
||||||
actual := observedState.filter(isService(service.Name))
|
actual := observedState.filter(isService(service.Name))
|
||||||
|
|
||||||
scale, err := getScale(service)
|
scale, err := getScale(service)
|
||||||
@ -87,6 +88,10 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if recreate == compose.RecreateNever {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
expected, err := jsonHash(service)
|
expected, err := jsonHash(service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -96,7 +101,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
|
|||||||
name := getCanonicalContainerName(container)
|
name := getCanonicalContainerName(container)
|
||||||
|
|
||||||
diverged := container.Labels[configHashLabel] != expected
|
diverged := container.Labels[configHashLabel] != expected
|
||||||
if diverged || service.Extensions[extLifecycle] == forceRecreate {
|
if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return s.recreateContainer(ctx, project, service, container)
|
return s.recreateContainer(ctx, project, service, container)
|
||||||
})
|
})
|
||||||
|
@ -95,7 +95,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
|
||||||
return s.ensureService(c, observedState, project, service)
|
return s.ensureService(c, observedState, project, service, opts.Recreate)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user