Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1485 from gtardif/fix_windows_engine_up
Browse files Browse the repository at this point in the history
Use old build for windows engine also on compose up
  • Loading branch information
ndeloof authored Apr 1, 2021
2 parents 487bf96 + d5efef2 commit cca87b5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 47 deletions.
25 changes: 12 additions & 13 deletions local/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
}
Expand Down
66 changes: 32 additions & 34 deletions local/compose/build_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit cca87b5

Please sign in to comment.