Merge pull request #9261 from ndeloof/recreate_on_image_updated

recreate container after image has been rebuilt/pulled
This commit is contained in:
Guillaume Lours 2022-03-10 12:11:02 +01:00 committed by GitHub
commit dc6097d1f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View File

@ -188,6 +188,15 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
for name, info := range imgs {
images[name] = info.ID
}
for _, s := range project.Services {
imgName := getImageName(s, project.Name)
digest, ok := images[imgName]
if ok {
s.CustomLabels[api.ImageDigestLabel] = digest
}
}
return images, nil
}

View File

@ -189,17 +189,11 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
continue
}
if recreate == api.RecreateNever {
continue
}
// Re-create diverged containers
configHash, err := ServiceHash(service)
mustRecreate, err := mustRecreate(service, container, recreate)
if err != nil {
return err
}
name := getContainerProgressName(container)
diverged := container.Labels[api.ConfigHashLabel] != configHash
if diverged || recreate == api.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
if mustRecreate {
i, container := i, container
eg.Go(func() error {
recreated, err := c.service.recreateContainer(ctx, project, service, container, inherit, timeout)
@ -211,6 +205,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
// Enforce non-diverged containers are running
w := progress.ContextWriter(ctx)
name := getContainerProgressName(container)
switch container.State {
case ContainerRunning:
w.Event(progress.RunningEvent(name))
@ -249,6 +244,22 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
return err
}
func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy string) (bool, error) {
if policy == api.RecreateNever {
return false, nil
}
if policy == api.RecreateForce || expected.Extensions[extLifecycle] == forceRecreate {
return true, nil
}
configHash, err := ServiceHash(expected)
if err != nil {
return false, err
}
configChanged := actual.Labels[api.ConfigHashLabel] != configHash
imageUpdated := actual.Labels[api.ImageDigestLabel] != expected.CustomLabels[api.ImageDigestLabel]
return configChanged || imageUpdated, nil
}
func getContainerName(projectName string, service types.ServiceConfig, number int) string {
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, Separator)
if service.ContainerName != "" {

View File

@ -93,7 +93,6 @@ func (s *composeService) getImages(ctx context.Context, images []string) (map[st
tag := ""
repository := ""
if len(inspect.RepoTags) > 0 {
repotag := strings.Split(inspect.RepoTags[0], ":")
repository = repotag[0]
if len(repotag) > 1 {