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

Commit e92b2f8

Browse files
aiordachegtardif
authored andcommitted
Shell out to docker cli for windows container builds
Signed-off-by: aiordache <[email protected]>
1 parent 95d2159 commit e92b2f8

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed

local/compose/build.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ import (
4141
func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
4242
opts := map[string]build.Options{}
4343
imagesToBuild := []string{}
44+
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+
4456
for _, service := range project.Services {
4557
if service.Build != nil {
4658
imageName := getImageName(service, project.Name)
@@ -66,7 +78,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
6678
}
6779
}
6880

69-
err := s.build(ctx, project, opts, options.Progress)
81+
err = s.build(ctx, project, opts, options.Progress)
7082
if err == nil {
7183
if len(imagesToBuild) > 0 {
7284
utils.DisplayScanSuggestMsg()

local/compose/build_win.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
24+
"github.com/docker/compose-cli/api/compose"
25+
"github.com/docker/compose-cli/local/moby"
26+
27+
"github.com/compose-spec/compose-go/types"
28+
)
29+
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+
}
41+
}
42+
// build args
43+
cmd := &commandBuilder{
44+
Path: filepath.Join(projectDir, service.Build.Context),
45+
}
46+
cmd.addParams("--build-arg", options.Args)
47+
cmd.addFlag("--pull", options.Pull)
48+
cmd.addArg("--progress", options.Progress)
49+
50+
cmd.addList("--cache-from", service.Build.CacheFrom)
51+
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+
59+
cmd.addArg("--tag", imageName)
60+
61+
args := cmd.getArguments()
62+
// shell out to moby cli
63+
err := moby.Exec(args)
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
}
69+
return nil
70+
}
71+
72+
type commandBuilder struct {
73+
Args []string
74+
Path string
75+
}
76+
77+
func (c *commandBuilder) addArg(name, value string) {
78+
if value != "" {
79+
c.Args = append(c.Args, name, value)
80+
}
81+
}
82+
83+
func (c *commandBuilder) addFlag(name string, flag bool) {
84+
if flag {
85+
c.Args = append(c.Args, name)
86+
}
87+
}
88+
89+
func (c *commandBuilder) addParams(name string, params map[string]string) {
90+
if len(params) > 0 {
91+
for k, v := range params {
92+
c.Args = append(c.Args, name, fmt.Sprintf("%s=%s", k, v))
93+
}
94+
}
95+
}
96+
97+
func (c *commandBuilder) addList(name string, values []string) {
98+
if len(values) > 0 {
99+
for _, v := range values {
100+
c.Args = append(c.Args, name, v)
101+
}
102+
}
103+
}
104+
105+
func (c *commandBuilder) getArguments() []string {
106+
cmd := []string{"build"}
107+
cmd = append(cmd, c.Args...)
108+
cmd = append(cmd, c.Path)
109+
return cmd
110+
}

local/moby/exec.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package moby
18+
19+
import (
20+
"os"
21+
"os/exec"
22+
"os/signal"
23+
24+
"github.com/docker/compose-cli/cli/mobycli/resolvepath"
25+
)
26+
27+
// ComDockerCli name of the classic cli binary
28+
const ComDockerCli = "com.docker.cli"
29+
30+
// Exec delegates to com.docker.cli
31+
func Exec(args []string) error {
32+
// look up the path of the classic cli binary
33+
execBinary, err := resolvepath.LookPath(ComDockerCli)
34+
if err != nil {
35+
return err
36+
}
37+
cmd := exec.Command(execBinary, args...)
38+
cmd.Stdin = os.Stdin
39+
cmd.Stdout = os.Stdout
40+
cmd.Stderr = os.Stderr
41+
42+
signals := make(chan os.Signal, 1)
43+
childExit := make(chan bool)
44+
signal.Notify(signals) // catch all signals
45+
go func() {
46+
for {
47+
select {
48+
case sig := <-signals:
49+
if cmd.Process == nil {
50+
continue // can happen if receiving signal before the process is actually started
51+
}
52+
// nolint errcheck
53+
cmd.Process.Signal(sig)
54+
case <-childExit:
55+
return
56+
}
57+
}
58+
}()
59+
err = cmd.Run()
60+
childExit <- true
61+
return err
62+
}

0 commit comments

Comments
 (0)