Set image digest as service image to trigger recreation after build

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-04-13 11:31:39 +02:00
parent d37dffe5e2
commit 31de84f547
3 changed files with 78 additions and 42 deletions

View File

@ -99,8 +99,15 @@ func runImages(ctx context.Context, opts imageOptions, services []string) error
for _, img := range images { for _, img := range images {
id := stringid.TruncateID(img.ID) id := stringid.TruncateID(img.ID)
size := units.HumanSizeWithPrecision(float64(img.Size), 3) size := units.HumanSizeWithPrecision(float64(img.Size), 3)
repo := img.Repository
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, img.Repository, img.Tag, id, size) if repo == "" {
repo = "<none>"
}
tag := img.Tag
if tag == "" {
tag = "<none>"
}
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
} }
}, },
"Container", "Repository", "Tag", "Image Id", "Size") "Container", "Repository", "Tag", "Image Id", "Size")

View File

@ -134,12 +134,39 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
} }
err := s.build(ctx, project, opts, observedState, mode) err := s.build(ctx, project, opts, observedState, mode)
if err == nil { if err != nil {
if len(imagesToBuild) > 0 { return err
utils.DisplayScanSuggestMsg() }
if len(imagesToBuild) > 0 {
utils.DisplayScanSuggestMsg()
}
return s.updateServiceImages(ctx, project)
}
func (s *composeService) updateServiceImages(ctx context.Context, project *types.Project) error {
imageNames := []string{}
for _, s := range project.Services {
imgName := s.Image
if imgName == "" {
imgName = getImageName(s, project.Name)
}
if !utils.StringContains(imageNames, imgName) {
imageNames = append(imageNames, imgName)
} }
} }
return err images, err := s.getImages(ctx, imageNames)
if err != nil {
return err
}
for i, s := range project.Services {
img, ok := images[getImageName(s, project.Name)]
if !ok {
return fmt.Errorf("failed to retrieve image for service %s", s.Name)
}
project.Services[i].Image = img.ID
}
return nil
} }
func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) { func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) {

View File

@ -57,25 +57,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
imageIDs = append(imageIDs, c.ImageID) imageIDs = append(imageIDs, c.ImageID)
} }
} }
images, err := s.getImages(ctx, imageIDs)
images := map[string]moby.ImageInspect{}
l := sync.Mutex{}
eg, ctx := errgroup.WithContext(ctx)
for _, img := range imageIDs {
img := img
eg.Go(func() error {
inspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, img)
if err != nil {
return err
}
l.Lock()
images[img] = inspect
l.Unlock()
return nil
})
}
err = eg.Wait()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -85,24 +67,44 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
if !ok { if !ok {
return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(container)) return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(container))
} }
if len(img.RepoTags) == 0 {
return nil, fmt.Errorf("no image tag found for %s", img.ID)
}
tag := ""
repository := ""
repotag := strings.Split(img.RepoTags[0], ":")
repository = repotag[0]
if len(repotag) > 1 {
tag = repotag[1]
}
summary[i] = compose.ImageSummary{ summary[i] = img
ID: img.ID, summary[i].ContainerName = getCanonicalContainerName(container)
ContainerName: getCanonicalContainerName(container),
Repository: repository,
Tag: tag,
Size: img.Size,
}
} }
return summary, nil return summary, nil
} }
func (s *composeService) getImages(ctx context.Context, images []string) (map[string]compose.ImageSummary, error) {
summary := map[string]compose.ImageSummary{}
l := sync.Mutex{}
eg, ctx := errgroup.WithContext(ctx)
for _, img := range images {
img := img
eg.Go(func() error {
inspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, img)
if err != nil {
return err
}
tag := ""
repository := ""
if len(inspect.RepoTags) > 0 {
repotag := strings.Split(inspect.RepoTags[0], ":")
repository = repotag[0]
if len(repotag) > 1 {
tag = repotag[1]
}
}
l.Lock()
summary[img] = compose.ImageSummary{
ID: inspect.ID,
Repository: repository,
Tag: tag,
Size: inspect.Size,
}
l.Unlock()
return nil
})
}
return summary, eg.Wait()
}