@@ -155,6 +155,28 @@ func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparse
155155
156156 // delete container if it already exists
157157 if existingCid != "" {
158+ // Default behavior for RecreateDiverged: compare stored hash with current service hash
159+ if recreate == RecreateDiverged {
160+ currentHash , err := ServiceHash (* service .Unparsed )
161+ if err != nil {
162+ return "" , fmt .Errorf ("failed computing service hash for %s: %w" , container .Name , err )
163+ }
164+ con , err := c .client .LoadContainer (ctx , existingCid )
165+ if err != nil {
166+ return "" , fmt .Errorf ("failed to load container %s: %w" , existingCid , err )
167+ }
168+ lbls , err := con .Labels (ctx )
169+ if err != nil {
170+ return "" , fmt .Errorf ("failed to read labels for %s: %w" , existingCid , err )
171+ }
172+ if lbls [labels .ComposeConfigHash ] == currentHash {
173+ cmd := c .createNerdctlCmd (ctx , append ([]string {"start" }, existingCid )... )
174+ if err := c .executeUpCmd (ctx , cmd , container .Name , runFlagD , service .Unparsed .StdinOpen ); err != nil {
175+ return "" , fmt .Errorf ("error while starting existing container %s: %w" , container .Name , err )
176+ }
177+ return existingCid , nil
178+ }
179+ }
158180 log .G (ctx ).Debugf ("Container %q already exists, deleting" , container .Name )
159181 delCmd := c .createNerdctlCmd (ctx , "rm" , "-f" , container .Name )
160182 if err = delCmd .Run (); err != nil {
@@ -184,10 +206,15 @@ func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparse
184206 }
185207
186208 //add metadata labels to container https://github.com/compose-spec/compose-spec/blob/master/spec.md#labels
209+ currentHash , err := ServiceHash (* service .Unparsed )
210+ if err != nil {
211+ return "" , fmt .Errorf ("failed computing service hash for %s: %w" , container .Name , err )
212+ }
187213 container .RunArgs = append ([]string {
188214 "--cidfile=" + cidFilename ,
189215 fmt .Sprintf ("-l=%s=%s" , labels .ComposeProject , c .project .Name ),
190216 fmt .Sprintf ("-l=%s=%s" , labels .ComposeService , service .Unparsed .Name ),
217+ fmt .Sprintf ("-l=%s=%s" , labels .ComposeConfigHash , currentHash ),
191218 }, container .RunArgs ... )
192219
193220 cmd := c .createNerdctlCmd (ctx , append ([]string {"run" }, container .RunArgs ... )... )
0 commit comments