88 "github.com/deislabs/cnab-go/bundle"
99 "github.com/docker/app/internal/packager"
1010 "github.com/docker/app/types"
11+ "github.com/docker/buildx/driver"
12+ "github.com/docker/buildx/util/progress"
13+ "github.com/docker/cli/cli/compose/loader"
1114 "github.com/docker/distribution/reference"
1215 "github.com/moby/buildkit/client"
1316 "github.com/moby/buildkit/session"
@@ -16,19 +19,15 @@ import (
1619 "github.com/sirupsen/logrus"
1720 "io/ioutil"
1821 "os"
22+ "path"
1923 "path/filepath"
20- "strings"
21-
22- "github.com/docker/buildx/driver"
23-
24- "github.com/docker/buildx/util/progress"
2524
2625 cnab "github.com/deislabs/cnab-go/driver"
27- "github.com/docker/buildx/bake"
2826 "github.com/docker/buildx/build"
2927 _ "github.com/docker/buildx/driver/docker" // required to get default driver registered, see driver/docker/factory.go:14
3028 "github.com/docker/cli/cli"
3129 "github.com/docker/cli/cli/command"
30+ composetypes "github.com/docker/cli/cli/compose/types"
3231 "github.com/moby/buildkit/util/appcontext"
3332 "github.com/spf13/cobra"
3433)
@@ -73,41 +72,23 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) (refe
7372 return nil , err
7473 }
7574 defer app .Cleanup ()
76- appname := app .Name
7775
7876 bundle , err := packager .MakeBundleFromApp (dockerCli , app , nil )
7977 if err != nil {
8078 return nil , err
8179 }
8280
83- targets , err := parseCompose (app )
81+ buildopts , err := parseCompose (app , opt )
8482 if err != nil {
8583 return nil , err
8684 }
8785
88- for service , t := range targets {
89- if strings .HasPrefix (* t .Context , "." ) {
90- // Relative path in compose file under x.dockerapp refers to parent folder
91- // FIXME docker app init should maybe update them ?
92- path , err := filepath .Abs (appname + "/../" + (* t .Context )[1 :])
93- if err != nil {
94- return nil , err
95- }
96- t .Context = & path
97- targets [service ] = t
98- }
99- }
100- debugTargets (targets )
101-
102- buildopts , err := bake .TargetsToBuildOpt (targets , opt .noCache , opt .pull )
86+ _ , err = createInvocationImageBuildOptions (dockerCli , app )
10387 if err != nil {
10488 return nil , err
10589 }
10690
107- buildopts ["invocation-image" ], err = createInvocationImageBuildOptions (dockerCli , app )
108- if err != nil {
109- return nil , err
110- }
91+ debugBuildOpts (buildopts )
11192
11293 ctx := appcontext .Context ()
11394 d , err := driver .GetDriver (ctx , "buildx_buildkit_default" , nil , dockerCli .Client (), nil , "" , nil )
@@ -206,11 +187,11 @@ func createInvocationImageBuildOptions(dockerCli command.Cli, app *types.App) (b
206187 }, nil
207188}
208189
209- func debugTargets ( targets map [string ]bake. Target ) {
190+ func debugBuildOpts ( opts map [string ]build. Options ) {
210191 if logrus .IsLevelEnabled (logrus .DebugLevel ) {
211- dt , err := json .MarshalIndent (targets , "" , " " )
192+ dt , err := json .MarshalIndent (opts , " > " , " " )
212193 if err != nil {
213- logrus .Debugf ("Failed to marshal Buildx response : %s" , err .Error ())
194+ logrus .Debugf ("Failed to marshal Bundle : %s" , err .Error ())
214195 } else {
215196 logrus .Debug (string (dt ))
216197 }
@@ -219,7 +200,7 @@ func debugTargets(targets map[string]bake.Target) {
219200
220201func debugBundle (bundle * bundle.Bundle ) {
221202 if logrus .IsLevelEnabled (logrus .DebugLevel ) {
222- dt , err := json .MarshalIndent (bundle , "" , " " )
203+ dt , err := json .MarshalIndent (bundle , " > " , " " )
223204 if err != nil {
224205 logrus .Debugf ("Failed to marshal Bundle: %s" , err .Error ())
225206 } else {
@@ -230,7 +211,7 @@ func debugBundle(bundle *bundle.Bundle) {
230211
231212func debugSolveResponses (resp map [string ]* client.SolveResponse ) {
232213 if logrus .IsLevelEnabled (logrus .DebugLevel ) {
233- dt , err := json .MarshalIndent (resp , "" , " " )
214+ dt , err := json .MarshalIndent (resp , " > " , " " )
234215 if err != nil {
235216 logrus .Debugf ("Failed to marshal Buildx response: %s" , err .Error ())
236217 } else {
@@ -240,23 +221,50 @@ func debugSolveResponses(resp map[string]*client.SolveResponse) {
240221}
241222
242223// parseCompose do parse app compose file and extract buildx targets
243- func parseCompose (app * types.App ) (map [string ]bake. Target , error ) {
244- compose , err := bake . ParseCompose ( app . Composes ()[ 0 ])
224+ func parseCompose (app * types.App , options buildOptions ) (map [string ]build. Options , error ) {
225+
245226 // Fixme can have > 1 composes ?
227+ parsed , err := loader .ParseYAML (app .Composes ()[0 ])
246228 if err != nil {
247229 return nil , err
248230 }
249- targets := map [string ]bake.Target {}
250- for _ , n := range compose .ResolveGroup ("default" ) {
251- t , err := compose .ResolveTarget (n )
252- if err != nil {
231+ compose , err := loader .Load (composetypes.ConfigDetails {
232+ ConfigFiles : []composetypes.ConfigFile {
233+ {
234+ Config : parsed ,
235+ },
236+ },
237+ })
238+ if err != nil {
239+ return nil , err
240+ }
241+
242+ opts := map [string ]build.Options {}
243+ for _ , service := range compose .Services {
244+ b := service .Build
245+ if b .Context == "" {
246+ continue
247+ }
248+
249+ if b .Context , err = filepath .Abs (b .Context ); err != nil {
253250 return nil , err
254251 }
255- if t != nil {
256- targets [n ] = * t
252+ dockerfilePath := "Dockerfile"
253+ if b .Dockerfile != "" {
254+ dockerfilePath = b .Dockerfile
255+ }
256+ dockerfilePath = path .Join (b .Context , dockerfilePath )
257+ opts [service .Name ] = build.Options {
258+ Inputs : build.Inputs {
259+ ContextPath : b .Context ,
260+ DockerfilePath : dockerfilePath ,
261+ },
262+ // BuildArgs: t.Args, //FIXME introduce build args support in docker app build
263+ NoCache : options .noCache ,
264+ Pull : options .pull ,
257265 }
258266 }
259- return targets , nil
267+ return opts , nil
260268}
261269
262270
0 commit comments