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

Commit d5efef2

Browse files
committed
Use old build for windows engine also on compose up
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent 487bf96 commit d5efef2

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

local/compose/build.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
4242
opts := map[string]build.Options{}
4343
imagesToBuild := []string{}
4444

45-
// retrieve OS type
46-
info, err := s.apiClient.Info(ctx)
47-
if err != nil {
48-
return err
49-
}
50-
if info.OSType == "windows" {
51-
// no support yet for Windows container builds in Buildkit
52-
// https://docs.docker.com/develop/develop-images/build_enhancements/#limitations
53-
return s.windowsBuild(project, options)
54-
}
55-
5645
for _, service := range project.Services {
5746
if service.Build != nil {
5847
imageName := getImageName(service, project.Name)
@@ -79,7 +68,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
7968
}
8069
}
8170

82-
err = s.build(ctx, project, opts, options.Progress)
71+
err := s.build(ctx, project, opts, options.Progress)
8372
if err == nil {
8473
if len(imagesToBuild) > 0 {
8574
utils.DisplayScanSuggestMsg()
@@ -128,7 +117,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
128117
DockerfilePath: "-",
129118
InStream: strings.NewReader("FROM " + service.Image),
130119
},
131-
Tags: []string{service.Image},
120+
Tags: []string{service.Image}, // Used to retrieve image to pull in case of windows engine
132121
Pull: true,
133122
}
134123

@@ -160,6 +149,16 @@ func (s *composeService) localImagePresent(ctx context.Context, imageName string
160149
}
161150

162151
func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) error {
152+
info, err := s.apiClient.Info(ctx)
153+
if err != nil {
154+
return err
155+
}
156+
157+
if info.OSType == "windows" {
158+
// no support yet for Windows container builds in Buildkit
159+
// https://docs.docker.com/develop/develop-images/build_enhancements/#limitations
160+
return s.windowsBuild(opts, mode)
161+
}
163162
if len(opts) == 0 {
164163
return nil
165164
}

local/compose/build_win.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,43 @@ package compose
1818

1919
import (
2020
"fmt"
21-
"os"
22-
"path/filepath"
2321

24-
"github.com/docker/compose-cli/api/compose"
22+
"github.com/docker/buildx/build"
2523
"github.com/docker/compose-cli/cli/mobycli"
26-
27-
"github.com/compose-spec/compose-go/types"
2824
)
2925

30-
func (s *composeService) windowsBuild(project *types.Project, options compose.BuildOptions) error {
31-
projectDir := project.WorkingDir
32-
for _, service := range project.Services {
33-
if service.Build != nil {
34-
imageName := getImageName(service, project.Name)
35-
dockerfile := service.Build.Dockerfile
36-
if dockerfile != "" {
37-
if stat, err := os.Stat(projectDir); err == nil && stat.IsDir() {
38-
39-
dockerfile = filepath.Join(projectDir, dockerfile)
40-
}
26+
func (s *composeService) windowsBuild(opts map[string]build.Options, mode string) error {
27+
for serviceName, options := range opts {
28+
imageName := serviceName
29+
dockerfile := options.Inputs.DockerfilePath
30+
31+
if options.Inputs.DockerfilePath == "-" { // image needs to be pulled
32+
imageName := options.Tags[0]
33+
err := shellOutMoby("pull", imageName)
34+
if err != nil {
35+
return err
4136
}
42-
// build args
37+
} else {
4338
cmd := &commandBuilder{
44-
Path: filepath.Join(projectDir, service.Build.Context),
39+
Path: options.Inputs.ContextPath,
4540
}
46-
cmd.addParams("--build-arg", options.Args)
41+
cmd.addParams("--build-arg", options.BuildArgs)
4742
cmd.addFlag("--pull", options.Pull)
48-
cmd.addArg("--progress", options.Progress)
43+
cmd.addArg("--progress", mode)
4944

50-
cmd.addList("--cache-from", service.Build.CacheFrom)
45+
cacheFrom := []string{}
46+
for _, cacheImage := range options.CacheFrom {
47+
cacheFrom = append(cacheFrom, cacheImage.Attrs["ref"])
48+
}
49+
cmd.addList("--cache-from", cacheFrom)
5150
cmd.addArg("--file", dockerfile)
52-
cmd.addParams("--label", service.Build.Labels)
53-
cmd.addArg("--network", service.Build.Network)
54-
cmd.addArg("--target", service.Build.Target)
55-
cmd.addArg("--platform", service.Platform)
56-
cmd.addArg("--isolation", service.Build.Isolation)
57-
cmd.addList("--add-host", service.Build.ExtraHosts)
58-
51+
cmd.addParams("--label", options.Labels)
52+
cmd.addArg("--network", options.NetworkMode)
53+
cmd.addArg("--target", options.Target)
54+
cmd.addList("--add-host", options.ExtraHosts)
5955
cmd.addArg("--tag", imageName)
6056

61-
args := cmd.getArguments()
62-
// shell out to moby cli
63-
childExit := make(chan bool)
64-
err := mobycli.RunDocker(childExit, args...)
65-
childExit <- true
66-
57+
err := shellOutMoby(cmd.getArguments()...)
6758
if err != nil {
6859
return err
6960
}
@@ -72,6 +63,13 @@ func (s *composeService) windowsBuild(project *types.Project, options compose.Bu
7263
return nil
7364
}
7465

66+
func shellOutMoby(args ...string) error {
67+
childExit := make(chan bool)
68+
err := mobycli.RunDocker(childExit, args...)
69+
childExit <- true
70+
return err
71+
}
72+
7573
type commandBuilder struct {
7674
Args []string
7775
Path string

0 commit comments

Comments
 (0)