inspect image ID after pull to se com.docker.compose.image

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2022-04-08 08:26:21 +02:00 committed by Nicolas De loof
parent 14ca112862
commit 00fd1c1530

View File

@ -95,7 +95,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
} }
eg.Go(func() error { eg.Go(func() error {
err := s.pullServiceImage(ctx, service, info, s.configFile(), w, false) _, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, false)
if err != nil { if err != nil {
if !opts.IgnoreFailures { if !opts.IgnoreFailures {
if service.Build != nil { if service.Build != nil {
@ -118,7 +118,7 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
return err return err
} }
func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPull bool) error { func (s *composeService) pullServiceImage(ctx context.Context, service types.ServiceConfig, info moby.Info, configFile driver.Auth, w progress.Writer, quietPull bool) (string, error) {
w.Event(progress.Event{ w.Event(progress.Event{
ID: service.Name, ID: service.Name,
Status: progress.Working, Status: progress.Working,
@ -126,12 +126,12 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
}) })
ref, err := reference.ParseNormalizedNamed(service.Image) ref, err := reference.ParseNormalizedNamed(service.Image)
if err != nil { if err != nil {
return err return "", err
} }
repoInfo, err := registry.ParseRepositoryInfo(ref) repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil { if err != nil {
return err return "", err
} }
key := repoInfo.Index.Name key := repoInfo.Index.Name
@ -141,12 +141,12 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
authConfig, err := configFile.GetAuthConfig(key) authConfig, err := configFile.GetAuthConfig(key)
if err != nil { if err != nil {
return err return "", err
} }
buf, err := json.Marshal(authConfig) buf, err := json.Marshal(authConfig)
if err != nil { if err != nil {
return err return "", err
} }
stream, err := s.apiClient().ImagePull(ctx, service.Image, moby.ImagePullOptions{ stream, err := s.apiClient().ImagePull(ctx, service.Image, moby.ImagePullOptions{
@ -159,7 +159,7 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
Status: progress.Error, Status: progress.Error,
Text: "Error", Text: "Error",
}) })
return WrapCategorisedComposeError(err, PullFailure) return "", WrapCategorisedComposeError(err, PullFailure)
} }
dec := json.NewDecoder(stream) dec := json.NewDecoder(stream)
@ -169,10 +169,10 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
if err == io.EOF { if err == io.EOF {
break break
} }
return WrapCategorisedComposeError(err, PullFailure) return "", WrapCategorisedComposeError(err, PullFailure)
} }
if jm.Error != nil { if jm.Error != nil {
return WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure) return "", WrapCategorisedComposeError(errors.New(jm.Error.Message), PullFailure)
} }
if !quietPull { if !quietPull {
toPullProgressEvent(service.Name, jm, w) toPullProgressEvent(service.Name, jm, w)
@ -183,7 +183,12 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
Status: progress.Done, Status: progress.Done,
Text: "Pulled", Text: "Pulled",
}) })
return nil
inspected, _, err := s.dockerCli.Client().ImageInspectWithRaw(ctx, service.Image)
if err != nil {
return "", err
}
return inspected.ID, nil
} }
func (s *composeService) pullRequiredImages(ctx context.Context, project *types.Project, images map[string]string, quietPull bool) error { func (s *composeService) pullRequiredImages(ctx context.Context, project *types.Project, images map[string]string, quietPull bool) error {
@ -220,10 +225,12 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
return progress.Run(ctx, func(ctx context.Context) error { return progress.Run(ctx, func(ctx context.Context) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
eg, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
for _, service := range needPull { pulledImages := make([]string, len(needPull))
service := service for i, service := range needPull {
i, service := i, service
eg.Go(func() error { eg.Go(func() error {
err := s.pullServiceImage(ctx, service, info, s.configFile(), w, quietPull) id, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, quietPull)
pulledImages[i] = id
if err != nil && service.Build != nil { if err != nil && service.Build != nil {
// image can be built, so we can ignore pull failure // image can be built, so we can ignore pull failure
return nil return nil
@ -231,7 +238,16 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
return err return err
}) })
} }
return eg.Wait() for i, service := range needPull {
if pulledImages[i] != "" {
images[service.Image] = pulledImages[i]
}
}
err := eg.Wait()
if err != nil {
return err
}
return err
}) })
} }