Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 9e77499

Browse files
committed
introduce --force-recreate and --no-recreate
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 657e894 commit 9e77499

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

api/compose/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type Service interface {
5757
type CreateOptions struct {
5858
// Remove legacy containers for services that are not defined in the project
5959
RemoveOrphans bool
60+
// Recreate define the strategy to apply on existing containers
61+
Recreate string
6062
}
6163

6264
// UpOptions group options of the Up API
@@ -137,6 +139,15 @@ const (
137139
FAILED string = "Failed"
138140
)
139141

142+
const (
143+
// RecreateDiverged to recreate services which configuration diverges from compose model
144+
RecreateDiverged = "diverged"
145+
// RecreateForce to force service container being recreated
146+
RecreateForce = "force"
147+
// RecreateNever to never recreate existing service containers
148+
RecreateNever = "never"
149+
)
150+
140151
// Stack holds the name and state of a compose application/stack
141152
type Stack struct {
142153
ID string

cli/cmd/compose/up.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ type upOptions struct {
4545
Detach bool
4646
Environment []string
4747
removeOrphans bool
48+
forceRecreate bool
49+
noRecreate bool
50+
}
51+
52+
func (o upOptions) recreateStrategy() string {
53+
if o.noRecreate {
54+
return compose.RecreateNever
55+
}
56+
if o.forceRecreate {
57+
return compose.RecreateForce
58+
}
59+
return compose.RecreateDiverged
4860
}
4961

5062
func upCommand(p *projectOptions, contextType string) *cobra.Command {
@@ -59,6 +71,9 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
5971
RunE: func(cmd *cobra.Command, args []string) error {
6072
switch contextType {
6173
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
74+
if opts.forceRecreate && opts.noRecreate {
75+
return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
76+
}
6277
return runCreateStart(cmd.Context(), opts, args)
6378
default:
6479
return runUp(cmd.Context(), opts, args)
@@ -71,8 +86,13 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
7186
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
7287
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
7388

74-
if contextType == store.AciContextType {
89+
switch contextType {
90+
case store.AciContextType:
7591
flags.StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
92+
case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
93+
flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
94+
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
95+
7696
}
7797

7898
return upCmd
@@ -101,6 +121,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
101121
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
102122
return "", c.ComposeService().Create(ctx, project, compose.CreateOptions{
103123
RemoveOrphans: opts.removeOrphans,
124+
Recreate: opts.recreateStrategy(),
104125
})
105126
})
106127
if err != nil {

local/compose/convergence.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/docker/docker/api/types/network"
2929
"golang.org/x/sync/errgroup"
3030

31+
"github.com/docker/compose-cli/api/compose"
3132
"github.com/docker/compose-cli/api/progress"
3233
status "github.com/docker/compose-cli/local/moby"
3334
)
@@ -74,7 +75,7 @@ func (s *composeService) ensureScale(ctx context.Context, actual []moby.Containe
7475
return eg, actual, nil
7576
}
7677

77-
func (s *composeService) ensureService(ctx context.Context, observedState Containers, project *types.Project, service types.ServiceConfig) error {
78+
func (s *composeService) ensureService(ctx context.Context, observedState Containers, project *types.Project, service types.ServiceConfig, recreate string) error {
7879
actual := observedState.filter(isService(service.Name))
7980

8081
scale, err := getScale(service)
@@ -87,6 +88,10 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
8788
return err
8889
}
8990

91+
if recreate == compose.RecreateNever {
92+
return nil
93+
}
94+
9095
expected, err := jsonHash(service)
9196
if err != nil {
9297
return err
@@ -96,7 +101,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
96101
name := getCanonicalContainerName(container)
97102

98103
diverged := container.Labels[configHashLabel] != expected
99-
if diverged || service.Extensions[extLifecycle] == forceRecreate {
104+
if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
100105
eg.Go(func() error {
101106
return s.recreateContainer(ctx, project, service, container)
102107
})

local/compose/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
9595
}
9696

9797
return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
98-
return s.ensureService(c, observedState, project, service)
98+
return s.ensureService(c, observedState, project, service, opts.Recreate)
9999
})
100100
}
101101

0 commit comments

Comments
 (0)