mirror of https://github.com/docker/compose.git
Merge pull request #1530 from aiordache/recreate_after_build
This commit is contained in:
commit
cd4a08d0d6
|
@ -99,8 +99,15 @@ func runImages(ctx context.Context, opts imageOptions, services []string) error
|
|||
for _, img := range images {
|
||||
id := stringid.TruncateID(img.ID)
|
||||
size := units.HumanSizeWithPrecision(float64(img.Size), 3)
|
||||
|
||||
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, img.Repository, img.Tag, id, size)
|
||||
repo := img.Repository
|
||||
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")
|
||||
|
|
|
@ -32,7 +32,6 @@ import (
|
|||
"github.com/docker/buildx/util/progress"
|
||||
cliconfig "github.com/docker/cli/cli/config"
|
||||
moby "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/errdefs"
|
||||
bclient "github.com/moby/buildkit/client"
|
||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
||||
|
@ -73,7 +72,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
|
|||
}
|
||||
}
|
||||
|
||||
err := s.build(ctx, project, opts, Containers{}, options.Progress)
|
||||
_, err := s.build(ctx, project, opts, Containers{}, options.Progress)
|
||||
if err == nil {
|
||||
if len(imagesToBuild) > 0 && !options.Quiet {
|
||||
utils.DisplayScanSuggestMsg()
|
||||
|
@ -84,28 +83,60 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
|
|||
}
|
||||
|
||||
func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project, observedState Containers, quietPull bool) error {
|
||||
images, err := s.getImageDigests(ctx, project)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode := progress.PrinterModeAuto
|
||||
if quietPull {
|
||||
mode = progress.PrinterModeQuiet
|
||||
}
|
||||
opts, imagesToBuild, err := s.getBuildOptions(project, images)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
builtImages, err := s.build(ctx, project, opts, observedState, mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(imagesToBuild) > 0 {
|
||||
utils.DisplayScanSuggestMsg()
|
||||
}
|
||||
for name, digest := range builtImages {
|
||||
images[name] = digest
|
||||
}
|
||||
// set digest as service.Image
|
||||
for i, service := range project.Services {
|
||||
digest, ok := images[getImageName(service, project.Name)]
|
||||
if ok {
|
||||
project.Services[i].Image = digest
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *composeService) getBuildOptions(project *types.Project, images map[string]string) (map[string]build.Options, []string, error) {
|
||||
opts := map[string]build.Options{}
|
||||
imagesToBuild := []string{}
|
||||
for _, service := range project.Services {
|
||||
if service.Image == "" && service.Build == nil {
|
||||
return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
|
||||
return nil, nil, fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
|
||||
}
|
||||
|
||||
imageName := getImageName(service, project.Name)
|
||||
localImagePresent, err := s.localImagePresent(ctx, imageName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, localImagePresent := images[imageName]
|
||||
|
||||
if service.Build != nil {
|
||||
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
|
||||
continue
|
||||
}
|
||||
imagesToBuild = append(imagesToBuild, imageName)
|
||||
opts[imageName], err = s.toBuildOptions(service, project.WorkingDir, imageName)
|
||||
opt, err := s.toBuildOptions(service, project.WorkingDir, imageName)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, nil, err
|
||||
}
|
||||
opts[imageName] = opt
|
||||
continue
|
||||
}
|
||||
if service.Image != "" {
|
||||
|
@ -113,7 +144,6 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
|
|||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Buildx has no command to "just pull", see
|
||||
// so we bake a temporary dockerfile that will just pull and export pulled image
|
||||
opts[service.Name] = build.Options{
|
||||
|
@ -127,57 +157,54 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
|
|||
}
|
||||
|
||||
}
|
||||
return opts, imagesToBuild, nil
|
||||
|
||||
mode := progress.PrinterModeAuto
|
||||
if quietPull {
|
||||
mode = progress.PrinterModeQuiet
|
||||
}
|
||||
|
||||
err := s.build(ctx, project, opts, observedState, mode)
|
||||
if err == nil {
|
||||
if len(imagesToBuild) > 0 {
|
||||
utils.DisplayScanSuggestMsg()
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) {
|
||||
_, _, err := s.apiClient.ImageInspectWithRaw(ctx, imageName)
|
||||
func (s *composeService) getImageDigests(ctx context.Context, project *types.Project) (map[string]string, error) {
|
||||
imageNames := []string{}
|
||||
for _, s := range project.Services {
|
||||
imgName := getImageName(s, project.Name)
|
||||
if !utils.StringContains(imageNames, imgName) {
|
||||
imageNames = append(imageNames, imgName)
|
||||
}
|
||||
}
|
||||
imgs, err := s.getImages(ctx, imageNames)
|
||||
if err != nil {
|
||||
if errdefs.IsNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
return true, nil
|
||||
images := map[string]string{}
|
||||
for name, info := range imgs {
|
||||
images[name] = info.ID
|
||||
}
|
||||
return images, nil
|
||||
}
|
||||
|
||||
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, observedState Containers, mode string) error {
|
||||
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, observedState Containers, mode string) (map[string]string, error) {
|
||||
info, err := s.apiClient.Info(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if info.OSType == "windows" {
|
||||
// no support yet for Windows container builds in Buildkit
|
||||
// https://docs.docker.com/develop/develop-images/build_enhancements/#limitations
|
||||
err := s.windowsBuild(opts, mode)
|
||||
return metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
||||
return nil, metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
||||
}
|
||||
if len(opts) == 0 {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
const drivername = "default"
|
||||
|
||||
configFile, err := cliconfig.Load(config.Dir())
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, configFile, nil, nil, "", nil, nil, project.WorkingDir)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
driverInfo := []build.DriverInfo{
|
||||
{
|
||||
|
@ -194,13 +221,13 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
|
|||
w := progress.NewPrinter(progressCtx, os.Stdout, mode)
|
||||
|
||||
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
|
||||
_, err = build.Build(ctx, driverInfo, opts, nil, nil, w)
|
||||
response, err := build.Build(ctx, driverInfo, opts, nil, nil, w)
|
||||
errW := w.Wait()
|
||||
if err == nil {
|
||||
err = errW
|
||||
}
|
||||
if err != nil {
|
||||
return metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
||||
return nil, metrics.WrapCategorisedComposeError(err, metrics.BuildFailure)
|
||||
}
|
||||
|
||||
cw := composeprogress.ContextWriter(ctx)
|
||||
|
@ -209,13 +236,25 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
|
|||
if c.Image == imageName {
|
||||
err = s.removeContainers(ctx, cw, []moby.Container{c}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
imagesBuilt := map[string]string{}
|
||||
for name, img := range response {
|
||||
if img == nil || len(img.ExporterResponse) == 0 {
|
||||
continue
|
||||
}
|
||||
digest, ok := img.ExporterResponse["containerimage.digest"]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
imagesBuilt[name] = digest
|
||||
}
|
||||
|
||||
return imagesBuilt, err
|
||||
}
|
||||
|
||||
func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) (build.Options, error) {
|
||||
|
|
|
@ -24,6 +24,7 @@ import (
|
|||
|
||||
moby "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/errdefs"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
|
@ -57,25 +58,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
|
|||
imageIDs = append(imageIDs, c.ImageID)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
images, err := s.getImages(ctx, imageIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -85,24 +68,47 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
|
|||
if !ok {
|
||||
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{
|
||||
ID: img.ID,
|
||||
ContainerName: getCanonicalContainerName(container),
|
||||
Repository: repository,
|
||||
Tag: tag,
|
||||
Size: img.Size,
|
||||
}
|
||||
summary[i] = img
|
||||
summary[i].ContainerName = getCanonicalContainerName(container)
|
||||
}
|
||||
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 {
|
||||
if errdefs.IsNotFound(err) {
|
||||
return 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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue