@@ -27,55 +27,23 @@ import (
27
27
"strings"
28
28
29
29
"github.com/compose-spec/compose-go/types"
30
- buildx "github.com/docker/buildx/build"
31
30
"github.com/docker/cli/cli"
32
31
"github.com/docker/cli/cli/command/image/build"
33
- "github.com/docker/compose/v2/pkg/utils"
34
32
dockertypes "github.com/docker/docker/api/types"
33
+ "github.com/docker/docker/api/types/container"
35
34
"github.com/docker/docker/builder/remotecontext/urlutil"
36
35
"github.com/docker/docker/pkg/archive"
37
36
"github.com/docker/docker/pkg/idtools"
38
37
"github.com/docker/docker/pkg/jsonmessage"
39
38
"github.com/docker/docker/pkg/progress"
40
39
"github.com/docker/docker/pkg/streamformatter"
41
- "github.com/hashicorp/go-multierror"
42
- "github.com/moby/buildkit/util/entitlements"
43
40
"github.com/pkg/errors"
44
41
45
42
"github.com/docker/compose/v2/pkg/api"
46
43
)
47
44
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
-
77
45
//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 ) {
79
47
var (
80
48
buildCtx io.ReadCloser
81
49
dockerfileCtx io.ReadCloser
@@ -86,31 +54,31 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
86
54
err error
87
55
)
88
56
89
- dockerfileName := options . Inputs . DockerfilePath
90
- specifiedContext := options . Inputs . ContextPath
57
+ dockerfileName := dockerFilePath ( service . Build . Context , service . Build . Dockerfile )
58
+ specifiedContext := service . Build . Context
91
59
progBuff := s .stdout ()
92
60
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
- }
99
61
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" )
102
70
}
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 " )
105
73
}
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" )
108
76
}
109
77
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 )
112
80
}
113
- options .Labels [api .ImageBuilderLabel ] = "classic"
81
+ service . Build .Labels [api .ImageBuilderLabel ] = "classic"
114
82
115
83
switch {
116
84
case isLocalDir (specifiedContext ):
@@ -189,8 +157,8 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
189
157
for k , auth := range creds {
190
158
authConfigs [k ] = dockertypes .AuthConfig (auth )
191
159
}
192
- buildOptions := imageBuildOptions (options )
193
- buildOptions .Version = dockertypes . BuilderV1
160
+ buildOptions := imageBuildOptions (service . Build )
161
+ buildOptions .Tags = append ( buildOptions . Tags , service . Image )
194
162
buildOptions .Dockerfile = relDockerfile
195
163
buildOptions .AuthConfigs = authConfigs
196
164
@@ -235,15 +203,6 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
235
203
"files and directories." )
236
204
}
237
205
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
-
247
206
return imageID , nil
248
207
}
249
208
@@ -252,25 +211,18 @@ func isLocalDir(c string) bool {
252
211
return err == nil
253
212
}
254
213
255
- func imageBuildOptions (options buildx. Options ) dockertypes.ImageBuildOptions {
214
+ func imageBuildOptions (config * types. BuildConfig ) dockertypes.ImageBuildOptions {
256
215
return dockertypes.ImageBuildOptions {
257
- Tags : options .Tags ,
258
- NoCache : options .NoCache ,
216
+ Version : dockertypes .BuilderV1 ,
217
+ Tags : config .Tags ,
218
+ NoCache : config .NoCache ,
259
219
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 ),
274
227
}
275
- return dest
276
228
}
0 commit comments