Skip to content

Commit f4ad2b8

Browse files
committed
build: Don't unpack by default when pushing
Automatically set `unpack=false` for registry exports unless explicitly overridden by the user. This applies to: - `registry` exporter type (converted to `image` exporter with `push=true`) - `--push` flag usage with image exporters Users can still explicitly set `unpack=true` if they need local image storage alongside registry push. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 45731b8 commit f4ad2b8

File tree

6 files changed

+94
-8
lines changed

6 files changed

+94
-8
lines changed

build/opt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,10 @@ func CreateExports(entries []*buildflags.ExportEntry) ([]client.ExportEntry, []s
835835
case "registry":
836836
out.Type = client.ExporterImage
837837
out.Attrs["push"] = "true"
838+
// Skip unpacking when only pushing to registry (unless explicitly set)
839+
if _, ok := out.Attrs["unpack"]; !ok {
840+
out.Attrs["unpack"] = "false"
841+
}
838842
}
839843

840844
if supportDir {

build/opt_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,78 @@ func TestCacheOptions_DerivedVars(t *testing.T) {
3838
},
3939
}, CreateCaches(cacheFrom))
4040
}
41+
42+
func TestCreateExports_RegistryUnpack(t *testing.T) {
43+
tests := []struct {
44+
name string
45+
entries []*buildflags.ExportEntry
46+
wantType string
47+
wantPush string
48+
wantUnpack string
49+
}{
50+
{
51+
name: "registry type sets unpack=false",
52+
entries: []*buildflags.ExportEntry{
53+
{
54+
Type: "registry",
55+
Attrs: map[string]string{},
56+
},
57+
},
58+
wantType: "image",
59+
wantPush: "true",
60+
wantUnpack: "false",
61+
},
62+
{
63+
name: "registry type respects explicit unpack=true",
64+
entries: []*buildflags.ExportEntry{
65+
{
66+
Type: "registry",
67+
Attrs: map[string]string{
68+
"unpack": "true",
69+
},
70+
},
71+
},
72+
wantType: "image",
73+
wantPush: "true",
74+
wantUnpack: "true",
75+
},
76+
{
77+
name: "registry type respects explicit unpack=false",
78+
entries: []*buildflags.ExportEntry{
79+
{
80+
Type: "registry",
81+
Attrs: map[string]string{
82+
"unpack": "false",
83+
},
84+
},
85+
},
86+
wantType: "image",
87+
wantPush: "true",
88+
wantUnpack: "false",
89+
},
90+
{
91+
name: "image type without push does not set unpack",
92+
entries: []*buildflags.ExportEntry{
93+
{
94+
Type: "image",
95+
Attrs: map[string]string{},
96+
},
97+
},
98+
wantType: "image",
99+
wantPush: "",
100+
wantUnpack: "",
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
exports, _, err := CreateExports(tt.entries)
107+
require.NoError(t, err)
108+
require.Len(t, exports, 1)
109+
110+
require.Equal(t, tt.wantType, exports[0].Type)
111+
require.Equal(t, tt.wantPush, exports[0].Attrs["push"])
112+
require.Equal(t, tt.wantUnpack, exports[0].Attrs["unpack"])
113+
})
114+
}
115+
}

commands/build.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugger debuggerOpt
541541

542542
flags.StringArrayVar(&options.platforms, "platform", platformsDefault, "Set target platform for build")
543543

544-
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--output=type=registry"`)
544+
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--output=type=registry,unpack=false"`)
545545

546546
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the build output and print image ID on success")
547547

@@ -1047,15 +1047,22 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in *BuildOptions, inSt
10471047
for i := range outputs {
10481048
if outputs[i].Type == client.ExporterImage {
10491049
outputs[i].Attrs["push"] = "true"
1050+
// Skip unpacking when only pushing to registry (unless explicitly set)
1051+
if _, ok := outputs[i].Attrs["unpack"]; !ok {
1052+
outputs[i].Attrs["unpack"] = "false"
1053+
}
10501054
pushUsed = true
10511055
}
10521056
}
10531057
if !pushUsed {
1058+
attrs := map[string]string{
1059+
"push": "true",
1060+
}
1061+
// Skip unpacking when only pushing to registry
1062+
attrs["unpack"] = "false"
10541063
outputs = append(outputs, client.ExportEntry{
1055-
Type: client.ExporterImage,
1056-
Attrs: map[string]string{
1057-
"push": "true",
1058-
},
1064+
Type: client.ExporterImage,
1065+
Attrs: attrs,
10591066
})
10601067
}
10611068
}

docs/reference/buildx_build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Start a build
4141
| [`--progress`](#progress) | `string` | `auto` | Set type of progress output (`auto`, `none`, `plain`, `quiet`, `rawjson`, `tty`). Use plain to show container output |
4242
| [`--provenance`](#provenance) | `string` | | Shorthand for `--attest=type=provenance` |
4343
| `--pull` | `bool` | | Always attempt to pull all referenced images |
44-
| [`--push`](#push) | `bool` | | Shorthand for `--output=type=registry` |
44+
| [`--push`](#push) | `bool` | | Shorthand for `--output=type=registry,unpack=false` |
4545
| `-q`, `--quiet` | `bool` | | Suppress the build output and print image ID on success |
4646
| [`--sbom`](#sbom) | `string` | | Shorthand for `--attest=type=sbom` |
4747
| [`--secret`](#secret) | `stringArray` | | Secret to expose to the build (format: `id=mysecret[,src=/local/secret]`) |

docs/reference/buildx_dap_build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Start a build
3333
| `--progress` | `string` | `auto` | Set type of progress output (`auto`, `none`, `plain`, `quiet`, `rawjson`, `tty`). Use plain to show container output |
3434
| `--provenance` | `string` | | Shorthand for `--attest=type=provenance` |
3535
| `--pull` | `bool` | | Always attempt to pull all referenced images |
36-
| `--push` | `bool` | | Shorthand for `--output=type=registry` |
36+
| `--push` | `bool` | | Shorthand for `--output=type=registry,unpack=false` |
3737
| `-q`, `--quiet` | `bool` | | Suppress the build output and print image ID on success |
3838
| `--sbom` | `string` | | Shorthand for `--attest=type=sbom` |
3939
| `--secret` | `stringArray` | | Secret to expose to the build (format: `id=mysecret[,src=/local/secret]`) |

docs/reference/buildx_debug_build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Start a build
3737
| `--progress` | `string` | `auto` | Set type of progress output (`auto`, `none`, `plain`, `quiet`, `rawjson`, `tty`). Use plain to show container output |
3838
| `--provenance` | `string` | | Shorthand for `--attest=type=provenance` |
3939
| `--pull` | `bool` | | Always attempt to pull all referenced images |
40-
| `--push` | `bool` | | Shorthand for `--output=type=registry` |
40+
| `--push` | `bool` | | Shorthand for `--output=type=registry,unpack=false` |
4141
| `-q`, `--quiet` | `bool` | | Suppress the build output and print image ID on success |
4242
| `--sbom` | `string` | | Shorthand for `--attest=type=sbom` |
4343
| `--secret` | `stringArray` | | Secret to expose to the build (format: `id=mysecret[,src=/local/secret]`) |

0 commit comments

Comments
 (0)