Skip to content

Commit

Permalink
Add support for classic builder
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Oct 22, 2021
1 parent 27f5b85 commit 0a037f8
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 77 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ github.com/moby/sys/mountinfo v0.1.3/go.mod h1:w2t2Avltqx8vE7gX5l+QiBKxODu2TX0+S
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
github.com/moby/sys/mountinfo v0.4.1 h1:1O+1cHA1aujwEwwVMa2Xm2l+gIpUHyd3+D+d7LZh1kM=
github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
github.com/moby/sys/symlink v0.1.0 h1:MTFZ74KtNI6qQQpuBxU+uKCim4WtOMokr03hCfJcazE=
github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ=
github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo=
github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf/go.mod h1:FBS0z0QWA44HXygs7VXDUOGoN/1TV3RuWkLO04am3wc=
Expand Down
60 changes: 11 additions & 49 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"github.com/compose-spec/compose-go/types"
"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
_ "github.com/docker/buildx/driver/docker" // required to get default driver registered
"github.com/docker/buildx/util/buildflags"
xprogress "github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
"github.com/docker/docker/client"
bclient "github.com/moby/buildkit/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
Expand Down Expand Up @@ -192,63 +194,23 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
}

func (s *composeService) doBuild(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
info, err := s.apiClient.Info(ctx)
if err != nil {
return nil, err
}

if info.OSType == "windows" {
// no support yet for Windows container builds in Buildkit
// https://docs.docker.com/develop/develop-images/build_enhancements/#limitations
err := s.windowsBuild(opts, mode)
return nil, WrapCategorisedComposeError(err, BuildFailure)
}
if len(opts) == 0 {
return nil, nil
}
const drivername = "default"

d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, s.configFile, nil, nil, "", nil, nil, project.WorkingDir)
dockerCli, err := command.NewDockerCli()
if err != nil {
return nil, err
}
driverInfo := []build.DriverInfo{
{
Name: "default",
Driver: d,
},
}

// Progress needs its own context that lives longer than the
// build one otherwise it won't read all the messages from
// build and will lock
progressCtx, cancel := context.WithCancel(context.Background())
defer cancel()
w := xprogress.NewPrinter(progressCtx, os.Stdout, mode)

// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
response, err := build.Build(ctx, driverInfo, opts, nil, nil, w)
errW := w.Wait()
if err == nil {
err = errW
}
err = dockerCli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
return s.apiClient, nil
}))
if err != nil {
return nil, WrapCategorisedComposeError(err, BuildFailure)
return nil, err
}

imagesBuilt := map[string]string{}
for name, img := range response {
if img == nil || len(img.ExporterResponse) == 0 {
continue
}
digest, ok := img.ExporterResponse["containerimage.digest"]
if !ok {
continue
}
imagesBuilt[name] = digest
if buildkitEnabled, err := command.BuildKitEnabled(dockerCli.ServerInfo()); err != nil || !buildkitEnabled {
return s.doBuildClassic(ctx, dockerCli, opts)
}

return imagesBuilt, err
return s.doBuildBuildkit(ctx, project, opts, mode)
}

func (s *composeService) toBuildOptions(project *types.Project, service types.ServiceConfig, imageTag string) (build.Options, error) {
Expand Down
72 changes: 72 additions & 0 deletions pkg/compose/build_buildkit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package compose

import (
"context"
"os"

"github.com/compose-spec/compose-go/types"
"github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
xprogress "github.com/docker/buildx/util/progress"
)

func (s *composeService) doBuildBuildkit(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
const drivername = "default"
d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, s.configFile, nil, nil, "", nil, nil, project.WorkingDir)
if err != nil {
return nil, err
}
driverInfo := []build.DriverInfo{
{
Name: drivername,
Driver: d,
},
}

// Progress needs its own context that lives longer than the
// build one otherwise it won't read all the messages from
// build and will lock
progressCtx, cancel := context.WithCancel(context.Background())
defer cancel()
w := xprogress.NewPrinter(progressCtx, os.Stdout, mode)

// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
response, err := build.Build(ctx, driverInfo, opts, nil, nil, w)
errW := w.Wait()
if err == nil {
err = errW
}
if err != nil {
return nil, WrapCategorisedComposeError(err, BuildFailure)
}

imagesBuilt := map[string]string{}
for name, img := range response {
if img == nil || len(img.ExporterResponse) == 0 {
continue
}
digest, ok := img.ExporterResponse["containerimage.digest"]
if !ok {
continue
}
imagesBuilt[name] = digest
}

return imagesBuilt, err
}
Loading

0 comments on commit 0a037f8

Please sign in to comment.