Skip to content

Commit b5ced03

Browse files
committed
introduce build --print to dump equivalent bakefile
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 0c5bd16 commit b5ced03

File tree

6 files changed

+22
-2
lines changed

6 files changed

+22
-2
lines changed

cmd/compose/build.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type buildOptions struct {
4444
ssh string
4545
builder string
4646
deps bool
47+
print bool
4748
}
4849

4950
func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
@@ -76,6 +77,8 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
7677
Quiet: opts.quiet,
7778
Services: services,
7879
Deps: opts.deps,
80+
Memory: int64(opts.memory),
81+
Print: opts.print,
7982
SSHs: SSHKeys,
8083
Builder: builderName,
8184
}, nil
@@ -131,6 +134,7 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service)
131134
flags.VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
132135
flags.StringVar(&p.Progress, "progress", string(buildkit.AutoMode), fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
133136
flags.MarkHidden("progress") //nolint:errcheck
137+
flags.BoolVar(&opts.print, "print", false, "Print equivalent bake file")
134138

135139
return cmd
136140
}
@@ -150,6 +154,5 @@ func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, o
150154
return err
151155
}
152156

153-
apiBuildOptions.Memory = int64(opts.memory)
154157
return backend.Build(ctx, project, apiBuildOptions)
155158
}

docs/reference/compose_build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ run `docker compose build` to rebuild it.
2020
| `--dry-run` | `bool` | | Execute command in dry run mode |
2121
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
2222
| `--no-cache` | `bool` | | Do not use cache when building the image |
23+
| `--print` | `bool` | | Print equivalent bake file |
2324
| `--pull` | `bool` | | Always attempt to pull a newer version of the image |
2425
| `--push` | `bool` | | Push service images |
2526
| `-q`, `--quiet` | `bool` | | Don't print anything to STDOUT |

docs/reference/docker_compose_build.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ options:
9696
experimentalcli: false
9797
kubernetes: false
9898
swarm: false
99+
- option: print
100+
value_type: bool
101+
default_value: "false"
102+
description: Print equivalent bake file
103+
deprecated: false
104+
hidden: false
105+
experimental: false
106+
experimentalcli: false
107+
kubernetes: false
108+
swarm: false
99109
- option: progress
100110
value_type: string
101111
default_value: auto

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ type BuildOptions struct {
155155
Memory int64
156156
// Builder name passed in the command line
157157
Builder string
158+
// Print don't actually run builder but print equivalent build config
159+
Print bool
158160
}
159161

160162
// Apply mutates project according to build options

pkg/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
105105
if err != nil {
106106
return nil, err
107107
}
108-
if bake {
108+
if bake || options.Print {
109109
trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "bake"))
110110
return s.doBuildBake(ctx, project, serviceToBuild, options)
111111
}

pkg/compose/build_bake.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
219219
return nil, err
220220
}
221221

222+
if options.Print {
223+
_, err = fmt.Fprintln(s.stdinfo(), string(b))
224+
return nil, err
225+
}
222226
logrus.Debugf("bake build config:\n%s", string(b))
223227

224228
metadata, err := os.CreateTemp(os.TempDir(), "compose")

0 commit comments

Comments
 (0)