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

Commit d67e8b9

Browse files
authored
Merge pull request #1474 from docker/build_windows
2 parents 95d2159 + 2b1158f commit d67e8b9

File tree

3 files changed

+151
-21
lines changed

3 files changed

+151
-21
lines changed

cli/mobycli/exec.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,40 @@ func mustDelegateToMoby(ctxType string) bool {
6262

6363
// Exec delegates to com.docker.cli if on moby context
6464
func Exec(root *cobra.Command) {
65+
childExit := make(chan bool)
66+
err := RunDocker(childExit, os.Args[1:]...)
67+
childExit <- true
68+
if err != nil {
69+
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
70+
71+
if exiterr, ok := err.(*exec.ExitError); ok {
72+
os.Exit(exiterr.ExitCode())
73+
}
74+
fmt.Fprintln(os.Stderr, err)
75+
os.Exit(1)
76+
}
77+
command := metrics.GetCommand(os.Args[1:])
78+
if command == "build" {
79+
utils.DisplayScanSuggestMsg()
80+
}
81+
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.SuccessStatus)
82+
83+
os.Exit(0)
84+
}
85+
86+
// RunDocker runs a docker command, and forward signals to the shellout command (stops listening to signals when an event is sent to childExit)
87+
func RunDocker(childExit chan bool, args ...string) error {
6588
execBinary, err := resolvepath.LookPath(ComDockerCli)
6689
if err != nil {
6790
fmt.Fprintln(os.Stderr, err)
6891
os.Exit(1)
6992
}
70-
cmd := exec.Command(execBinary, os.Args[1:]...)
93+
cmd := exec.Command(execBinary, args...)
7194
cmd.Stdin = os.Stdin
7295
cmd.Stdout = os.Stdout
7396
cmd.Stderr = os.Stderr
7497

7598
signals := make(chan os.Signal, 1)
76-
childExit := make(chan bool)
7799
signal.Notify(signals) // catch all signals
78100
go func() {
79101
for {
@@ -90,24 +112,7 @@ func Exec(root *cobra.Command) {
90112
}
91113
}()
92114

93-
err = cmd.Run()
94-
childExit <- true
95-
if err != nil {
96-
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
97-
98-
if exiterr, ok := err.(*exec.ExitError); ok {
99-
os.Exit(exiterr.ExitCode())
100-
}
101-
fmt.Fprintln(os.Stderr, err)
102-
os.Exit(1)
103-
}
104-
command := metrics.GetCommand(os.Args[1:])
105-
if command == "build" {
106-
utils.DisplayScanSuggestMsg()
107-
}
108-
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.SuccessStatus)
109-
110-
os.Exit(0)
115+
return cmd.Run()
111116
}
112117

113118
// IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)

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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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/cli/mobycli"
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+
childExit := make(chan bool)
64+
err := mobycli.RunDocker(childExit, args...)
65+
childExit <- true
66+
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
}
72+
return nil
73+
}
74+
75+
type commandBuilder struct {
76+
Args []string
77+
Path string
78+
}
79+
80+
func (c *commandBuilder) addArg(name, value string) {
81+
if value != "" {
82+
c.Args = append(c.Args, name, value)
83+
}
84+
}
85+
86+
func (c *commandBuilder) addFlag(name string, flag bool) {
87+
if flag {
88+
c.Args = append(c.Args, name)
89+
}
90+
}
91+
92+
func (c *commandBuilder) addParams(name string, params map[string]string) {
93+
if len(params) > 0 {
94+
for k, v := range params {
95+
c.Args = append(c.Args, name, fmt.Sprintf("%s=%s", k, v))
96+
}
97+
}
98+
}
99+
100+
func (c *commandBuilder) addList(name string, values []string) {
101+
if len(values) > 0 {
102+
for _, v := range values {
103+
c.Args = append(c.Args, name, v)
104+
}
105+
}
106+
}
107+
108+
func (c *commandBuilder) getArguments() []string {
109+
cmd := []string{"build"}
110+
cmd = append(cmd, c.Args...)
111+
cmd = append(cmd, c.Path)
112+
return cmd
113+
}

0 commit comments

Comments
 (0)