@@ -21,7 +21,6 @@ import (
2121 "errors"
2222 "fmt"
2323 "os"
24- "path/filepath"
2524
2625 "github.com/compose-spec/compose-go/v2/types"
2726 "github.com/containerd/platforms"
@@ -38,7 +37,6 @@ import (
3837 "github.com/docker/compose/v2/pkg/progress"
3938 "github.com/docker/compose/v2/pkg/utils"
4039 "github.com/docker/docker/api/types/container"
41- "github.com/docker/docker/builder/remotecontext/urlutil"
4240 bclient "github.com/moby/buildkit/client"
4341 "github.com/moby/buildkit/session"
4442 "github.com/moby/buildkit/session/auth/authprovider"
@@ -64,26 +62,16 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
6462 }, s .stdinfo (), "Building" )
6563}
6664
67- type serviceToBuild struct {
68- name string
69- service types.ServiceConfig
70- }
71-
7265//nolint:gocyclo
7366func (s * composeService ) build (ctx context.Context , project * types.Project , options api.BuildOptions , localImages map [string ]string ) (map [string ]string , error ) {
74- buildkitEnabled , err := s .dockerCli .BuildKitEnabled ()
75- if err != nil {
76- return nil , err
77- }
78-
7967 imageIDs := map [string ]string {}
80- serviceToBeBuild := map [ string ] serviceToBuild {}
68+ serviceToBuild := types. Services {}
8169
8270 var policy types.DependencyOption = types .IgnoreDependencies
8371 if options .Deps {
8472 policy = types .IncludeDependencies
8573 }
86- err = project .ForEachService (options .Services , func (serviceName string , service * types.ServiceConfig ) error {
74+ err : = project .ForEachService (options .Services , func (serviceName string , service * types.ServiceConfig ) error {
8775 if service .Build == nil {
8876 return nil
8977 }
@@ -92,14 +80,26 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
9280 if localImagePresent && service .PullPolicy != types .PullPolicyBuild {
9381 return nil
9482 }
95- serviceToBeBuild [serviceName ] = serviceToBuild { name : serviceName , service : * service }
83+ serviceToBuild [serviceName ] = * service
9684 return nil
9785 }, policy )
98- if err != nil || len (serviceToBeBuild ) == 0 {
86+ if err != nil || len (serviceToBuild ) == 0 {
9987 return imageIDs , err
10088 }
10189
90+ bake , err := buildWithBake (s .dockerCli )
91+ if err != nil {
92+ return nil , err
93+ }
94+ if bake {
95+ return s .doBuildBake (ctx , project , serviceToBuild , options )
96+ }
97+
10298 // Initialize buildkit nodes
99+ buildkitEnabled , err := s .dockerCli .BuildKitEnabled ()
100+ if err != nil {
101+ return nil , err
102+ }
103103 var (
104104 b * builder.Builder
105105 nodes []builder.Node
@@ -152,12 +152,10 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
152152 return - 1
153153 }
154154 err = InDependencyOrder (ctx , project , func (ctx context.Context , name string ) error {
155- serviceToBuild , ok := serviceToBeBuild [name ]
155+ service , ok := serviceToBuild [name ]
156156 if ! ok {
157157 return nil
158158 }
159- service := serviceToBuild .service
160-
161159 cw := progress .ContextWriter (ctx )
162160 serviceName := fmt .Sprintf ("Service %s" , name )
163161
@@ -211,7 +209,8 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
211209
212210 for i , imageDigest := range builtDigests {
213211 if imageDigest != "" {
214- imageRef := api .GetImageNameOrDefault (project.Services [names [i ]], project .Name )
212+ service := project.Services [names [i ]]
213+ imageRef := api .GetImageNameOrDefault (service , project .Name )
215214 imageIDs [imageRef ] = imageDigest
216215 }
217216 }
@@ -334,12 +333,7 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
334333//
335334// Finally, standard proxy variables based on the Docker client configuration are added, but will not overwrite
336335// any values if already present.
337- func resolveAndMergeBuildArgs (
338- dockerCli command.Cli ,
339- project * types.Project ,
340- service types.ServiceConfig ,
341- opts api.BuildOptions ,
342- ) types.MappingWithEquals {
336+ func resolveAndMergeBuildArgs (dockerCli command.Cli , project * types.Project , service types.ServiceConfig , opts api.BuildOptions ) types.MappingWithEquals {
343337 result := make (types.MappingWithEquals ).
344338 OverrideBy (service .Build .Args ).
345339 OverrideBy (opts .Args ).
@@ -479,16 +473,6 @@ func flatten(in types.MappingWithEquals) types.Mapping {
479473 return out
480474}
481475
482- func dockerFilePath (ctxName string , dockerfile string ) string {
483- if dockerfile == "" {
484- return ""
485- }
486- if urlutil .IsGitURL (ctxName ) || filepath .IsAbs (dockerfile ) {
487- return dockerfile
488- }
489- return filepath .Join (ctxName , dockerfile )
490- }
491-
492476func sshAgentProvider (sshKeys types.SSHConfig ) (session.Attachable , error ) {
493477 sshConfig := make ([]sshprovider.AgentConfig , 0 , len (sshKeys ))
494478 for _ , sshKey := range sshKeys {
0 commit comments