@@ -27,55 +27,23 @@ import (
2727 "strings"
2828
2929 "github.com/compose-spec/compose-go/types"
30- buildx "github.com/docker/buildx/build"
3130 "github.com/docker/cli/cli"
3231 "github.com/docker/cli/cli/command/image/build"
33- "github.com/docker/compose/v2/pkg/utils"
3432 dockertypes "github.com/docker/docker/api/types"
33+ "github.com/docker/docker/api/types/container"
3534 "github.com/docker/docker/builder/remotecontext/urlutil"
3635 "github.com/docker/docker/pkg/archive"
3736 "github.com/docker/docker/pkg/idtools"
3837 "github.com/docker/docker/pkg/jsonmessage"
3938 "github.com/docker/docker/pkg/progress"
4039 "github.com/docker/docker/pkg/streamformatter"
41- "github.com/hashicorp/go-multierror"
42- "github.com/moby/buildkit/util/entitlements"
4340 "github.com/pkg/errors"
4441
4542 "github.com/docker/compose/v2/pkg/api"
4643)
4744
48- func (s * composeService ) doBuildClassic (ctx context.Context , project * types.Project , opts map [string ]buildx.Options ) (map [string ]string , error ) {
49- nameDigests := make (map [string ]string )
50- var errs error
51- err := project .WithServices (nil , func (service types.ServiceConfig ) error {
52- imageName := api .GetImageNameOrDefault (service , project .Name )
53- o , ok := opts [imageName ]
54- if ! ok {
55- return nil
56- }
57- digest , err := s .doBuildClassicSimpleImage (ctx , o )
58- if err != nil {
59- errs = multierror .Append (errs , err ).ErrorOrNil ()
60- }
61- nameDigests [imageName ] = digest
62- if errs != nil {
63- return nil
64- }
65- if len (o .Exports ) != 0 && o .Exports [0 ].Attrs ["push" ] == "true" {
66- return s .push (ctx , project , api.PushOptions {})
67- }
68- return nil
69- })
70- if err != nil {
71- return nil , err
72- }
73-
74- return nameDigests , errs
75- }
76-
7745//nolint:gocyclo
78- func (s * composeService ) doBuildClassicSimpleImage (ctx context.Context , options buildx. Options ) (string , error ) {
46+ func (s * composeService ) doBuildClassic (ctx context.Context , service types. ServiceConfig ) (string , error ) {
7947 var (
8048 buildCtx io.ReadCloser
8149 dockerfileCtx io.ReadCloser
@@ -86,31 +54,31 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
8654 err error
8755 )
8856
89- dockerfileName := options . Inputs . DockerfilePath
90- specifiedContext := options . Inputs . ContextPath
57+ dockerfileName := dockerFilePath ( service . Build . Context , service . Build . Dockerfile )
58+ specifiedContext := service . Build . Context
9159 progBuff := s .stdout ()
9260 buildBuff := s .stdout ()
93- if options .ImageIDFile != "" {
94- // Avoid leaving a stale file if we eventually fail
95- if err := os .Remove (options .ImageIDFile ); err != nil && ! os .IsNotExist (err ) {
96- return "" , errors .Wrap (err , "removing image ID file" )
97- }
98- }
9961
100- if len (options .Platforms ) > 1 {
101- return "" , errors .Errorf ("this builder doesn't support multi-arch build, set DOCKER_BUILDKIT=1 to use multi-arch builder" )
62+ if len (service .Build .Platforms ) > 1 {
63+ return "" , errors .Errorf ("the classic builder doesn't support multi-arch build, set DOCKER_BUILDKIT=1 to use BuildKit" )
64+ }
65+ if service .Build .Privileged {
66+ return "" , errors .Errorf ("the classic builder doesn't support privileged mode, set DOCKER_BUILDKIT=1 to use BuildKit" )
67+ }
68+ if len (service .Build .AdditionalContexts ) > 0 {
69+ return "" , errors .Errorf ("the classic builder doesn't support additional contexts, set DOCKER_BUILDKIT=1 to use BuildKit" )
10270 }
103- if utils . Contains ( options . Allow , entitlements . EntitlementSecurityInsecure ) {
104- return "" , errors .Errorf ("this builder doesn't support privileged mode , set DOCKER_BUILDKIT=1 to use builder supporting privileged mode " )
71+ if len ( service . Build . SSH ) > 0 {
72+ return "" , errors .Errorf ("the classic builder doesn't support SSH keys , set DOCKER_BUILDKIT=1 to use BuildKit " )
10573 }
106- if len (options . Inputs . NamedContexts ) > 0 {
107- return "" , errors .Errorf ("this builder doesn't support additional contexts , set DOCKER_BUILDKIT=1 to use BuildKit which does " )
74+ if len (service . Build . Secrets ) > 0 {
75+ return "" , errors .Errorf ("the classic builder doesn't support secrets , set DOCKER_BUILDKIT=1 to use BuildKit" )
10876 }
10977
110- if options .Labels == nil {
111- options .Labels = make (map [string ]string )
78+ if service . Build .Labels == nil {
79+ service . Build .Labels = make (map [string ]string )
11280 }
113- options .Labels [api .ImageBuilderLabel ] = "classic"
81+ service . Build .Labels [api .ImageBuilderLabel ] = "classic"
11482
11583 switch {
11684 case isLocalDir (specifiedContext ):
@@ -189,8 +157,8 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
189157 for k , auth := range creds {
190158 authConfigs [k ] = dockertypes .AuthConfig (auth )
191159 }
192- buildOptions := imageBuildOptions (options )
193- buildOptions .Version = dockertypes . BuilderV1
160+ buildOptions := imageBuildOptions (service . Build )
161+ buildOptions .Tags = append ( buildOptions . Tags , service . Image )
194162 buildOptions .Dockerfile = relDockerfile
195163 buildOptions .AuthConfigs = authConfigs
196164
@@ -235,15 +203,6 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
235203 "files and directories." )
236204 }
237205
238- if options .ImageIDFile != "" {
239- if imageID == "" {
240- return "" , errors .Errorf ("Server did not provide an image ID. Cannot write %s" , options .ImageIDFile )
241- }
242- if err := os .WriteFile (options .ImageIDFile , []byte (imageID ), 0o666 ); err != nil {
243- return "" , err
244- }
245- }
246-
247206 return imageID , nil
248207}
249208
@@ -252,25 +211,18 @@ func isLocalDir(c string) bool {
252211 return err == nil
253212}
254213
255- func imageBuildOptions (options buildx. Options ) dockertypes.ImageBuildOptions {
214+ func imageBuildOptions (config * types. BuildConfig ) dockertypes.ImageBuildOptions {
256215 return dockertypes.ImageBuildOptions {
257- Tags : options .Tags ,
258- NoCache : options .NoCache ,
216+ Version : dockertypes .BuilderV1 ,
217+ Tags : config .Tags ,
218+ NoCache : config .NoCache ,
259219 Remove : true ,
260- PullParent : options .Pull ,
261- BuildArgs : toMapStringStringPtr (options .BuildArgs ),
262- Labels : options .Labels ,
263- NetworkMode : options .NetworkMode ,
264- ExtraHosts : options .ExtraHosts ,
265- Target : options .Target ,
266- }
267- }
268-
269- func toMapStringStringPtr (source map [string ]string ) map [string ]* string {
270- dest := make (map [string ]* string )
271- for k , v := range source {
272- v := v
273- dest [k ] = & v
220+ PullParent : config .Pull ,
221+ BuildArgs : config .Args ,
222+ Labels : config .Labels ,
223+ NetworkMode : config .Network ,
224+ ExtraHosts : config .ExtraHosts .AsList (),
225+ Target : config .Target ,
226+ Isolation : container .Isolation (config .Isolation ),
274227 }
275- return dest
276228}
0 commit comments