diff --git a/local/compose/build.go b/local/compose/build.go index e36e22ae7..058ed42b7 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -42,17 +42,6 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti opts := map[string]build.Options{} imagesToBuild := []string{} - // retrieve OS type - info, err := s.apiClient.Info(ctx) - if err != nil { - return err - } - if info.OSType == "windows" { - // no support yet for Windows container builds in Buildkit - // https://docs.docker.com/develop/develop-images/build_enhancements/#limitations - return s.windowsBuild(project, options) - } - for _, service := range project.Services { if service.Build != nil { imageName := getImageName(service, project.Name) @@ -79,7 +68,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti } } - err = s.build(ctx, project, opts, options.Progress) + err := s.build(ctx, project, opts, options.Progress) if err == nil { if len(imagesToBuild) > 0 { utils.DisplayScanSuggestMsg() @@ -128,7 +117,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. DockerfilePath: "-", InStream: strings.NewReader("FROM " + service.Image), }, - Tags: []string{service.Image}, + Tags: []string{service.Image}, // Used to retrieve image to pull in case of windows engine Pull: true, } @@ -160,6 +149,16 @@ func (s *composeService) localImagePresent(ctx context.Context, imageName string } func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) error { + info, err := s.apiClient.Info(ctx) + if err != nil { + return err + } + + if info.OSType == "windows" { + // no support yet for Windows container builds in Buildkit + // https://docs.docker.com/develop/develop-images/build_enhancements/#limitations + return s.windowsBuild(opts, mode) + } if len(opts) == 0 { return nil } diff --git a/local/compose/build_win.go b/local/compose/build_win.go index 431eeed01..72dba122d 100644 --- a/local/compose/build_win.go +++ b/local/compose/build_win.go @@ -18,52 +18,43 @@ package compose import ( "fmt" - "os" - "path/filepath" - "github.com/docker/compose-cli/api/compose" + "github.com/docker/buildx/build" "github.com/docker/compose-cli/cli/mobycli" - - "github.com/compose-spec/compose-go/types" ) -func (s *composeService) windowsBuild(project *types.Project, options compose.BuildOptions) error { - projectDir := project.WorkingDir - for _, service := range project.Services { - if service.Build != nil { - imageName := getImageName(service, project.Name) - dockerfile := service.Build.Dockerfile - if dockerfile != "" { - if stat, err := os.Stat(projectDir); err == nil && stat.IsDir() { - - dockerfile = filepath.Join(projectDir, dockerfile) - } +func (s *composeService) windowsBuild(opts map[string]build.Options, mode string) error { + for serviceName, options := range opts { + imageName := serviceName + dockerfile := options.Inputs.DockerfilePath + + if options.Inputs.DockerfilePath == "-" { // image needs to be pulled + imageName := options.Tags[0] + err := shellOutMoby("pull", imageName) + if err != nil { + return err } - // build args + } else { cmd := &commandBuilder{ - Path: filepath.Join(projectDir, service.Build.Context), + Path: options.Inputs.ContextPath, } - cmd.addParams("--build-arg", options.Args) + cmd.addParams("--build-arg", options.BuildArgs) cmd.addFlag("--pull", options.Pull) - cmd.addArg("--progress", options.Progress) + cmd.addArg("--progress", mode) - cmd.addList("--cache-from", service.Build.CacheFrom) + cacheFrom := []string{} + for _, cacheImage := range options.CacheFrom { + cacheFrom = append(cacheFrom, cacheImage.Attrs["ref"]) + } + cmd.addList("--cache-from", cacheFrom) cmd.addArg("--file", dockerfile) - cmd.addParams("--label", service.Build.Labels) - cmd.addArg("--network", service.Build.Network) - cmd.addArg("--target", service.Build.Target) - cmd.addArg("--platform", service.Platform) - cmd.addArg("--isolation", service.Build.Isolation) - cmd.addList("--add-host", service.Build.ExtraHosts) - + cmd.addParams("--label", options.Labels) + cmd.addArg("--network", options.NetworkMode) + cmd.addArg("--target", options.Target) + cmd.addList("--add-host", options.ExtraHosts) cmd.addArg("--tag", imageName) - args := cmd.getArguments() - // shell out to moby cli - childExit := make(chan bool) - err := mobycli.RunDocker(childExit, args...) - childExit <- true - + err := shellOutMoby(cmd.getArguments()...) if err != nil { return err } @@ -72,6 +63,13 @@ func (s *composeService) windowsBuild(project *types.Project, options compose.Bu return nil } +func shellOutMoby(args ...string) error { + childExit := make(chan bool) + err := mobycli.RunDocker(childExit, args...) + childExit <- true + return err +} + type commandBuilder struct { Args []string Path string