Skip to content

Commit 927b769

Browse files
Merge pull request #436 from depot/pedro/dep-3616-fix-depot-bake-load-tarball-fallback
fix: pass build platform to Docker pull to avoid tarball fallback
2 parents 6edee0b + a1d6de5 commit 927b769

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

pkg/buildx/commands/bake.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator, pri
123123
buildOpts, pullOpts = load.WithDepotImagePull(
124124
buildOpts,
125125
load.DepotLoadOptions{
126-
Project: in.DepotOptions.project,
127-
BuildID: in.DepotOptions.buildID,
128-
IsBake: true,
129-
ProgressMode: in.progress,
130-
UseRegistry: in.DepotOptions.loadUsingRegistry,
131-
PullInfo: in.DepotOptions.pullInfo,
126+
Project: in.DepotOptions.project,
127+
BuildID: in.DepotOptions.buildID,
128+
IsBake: true,
129+
ProgressMode: in.progress,
130+
UseRegistry: in.DepotOptions.loadUsingRegistry,
131+
PullInfo: in.DepotOptions.pullInfo,
132+
BuildPlatform: in.DepotOptions.buildPlatform,
132133
},
133134
)
134135
}

pkg/buildx/commands/build.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,13 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, nodes []builder.No
230230
opts, pullOpts = load.WithDepotImagePull(
231231
opts,
232232
load.DepotLoadOptions{
233-
Project: depotOpts.project,
234-
BuildID: depotOpts.buildID,
235-
IsBake: false,
236-
ProgressMode: progressMode,
237-
UseRegistry: depotOpts.loadUsingRegistry,
238-
PullInfo: depotOpts.pullInfo,
233+
Project: depotOpts.project,
234+
BuildID: depotOpts.buildID,
235+
IsBake: false,
236+
ProgressMode: progressMode,
237+
UseRegistry: depotOpts.loadUsingRegistry,
238+
PullInfo: depotOpts.pullInfo,
239+
BuildPlatform: depotOpts.buildPlatform,
239240
},
240241
)
241242
}

pkg/load/cli.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import (
1313

1414
// DepotLoadOptions are options to load images from the depot hosted registry.
1515
type DepotLoadOptions struct {
16-
Project string // Depot project name; used to tag images.
17-
BuildID string // Depot build ID; used to tag images.
18-
IsBake bool // If run from bake, we add the bake target to the image tag.
19-
ProgressMode string // ProgressMode quiet will not print progress.
20-
UseRegistry bool // If UseRegistry, load build from registry instead of proxy
21-
PullInfo *depotbuild.PullInfo // If UseRegistry, the credentials for pulling from registry
16+
Project string // Depot project name; used to tag images.
17+
BuildID string // Depot build ID; used to tag images.
18+
IsBake bool // If run from bake, we add the bake target to the image tag.
19+
ProgressMode string // ProgressMode quiet will not print progress.
20+
UseRegistry bool // If UseRegistry, load build from registry instead of proxy
21+
PullInfo *depotbuild.PullInfo // If UseRegistry, the credentials for pulling from registry
22+
BuildPlatform string // If set (e.g. "linux/amd64"), used as the pull platform when buildOpt.Platforms is empty.
2223
}
2324

2425
// Options to download from the Depot hosted registry and tag the image with the user provide tag.
@@ -74,19 +75,28 @@ func WithDepotImagePull(buildOpts map[string]build.Options, loadOpts DepotLoadOp
7475
Quiet: loadOpts.ProgressMode == progress.PrinterModeQuiet,
7576
}
7677

78+
// Specify a platform to pull when a single platform is used.
79+
// This ensures Docker pulls the correct architecture manifest
80+
// even when the build platform differs from the host (e.g.
81+
// building amd64 on an arm64 Mac), avoiding a slow tarball
82+
// fallback during fast load.
83+
platforms := platformutil.Format(buildOpt.Platforms)
84+
if len(platforms) == 1 {
85+
platform := platforms[0]
86+
pullOpt.Platform = &platform
87+
} else if len(platforms) == 0 && loadOpts.BuildPlatform != "" {
88+
// When --build-platform is specified but platforms is not set in the
89+
// compose file, use the build platform so Docker pulls the correct
90+
// architecture instead of defaulting to the host architecture.
91+
pullOpt.Platform = &loadOpts.BuildPlatform
92+
}
93+
7794
if loadOpts.UseRegistry && loadOpts.PullInfo != nil {
7895
serverAddress := "registry.depot.dev"
7996
pullOpt.KeepImage = true
8097
pullOpt.Username = &loadOpts.PullInfo.Username
8198
pullOpt.Password = &loadOpts.PullInfo.Password
8299
pullOpt.ServerAddress = &serverAddress
83-
84-
platforms := platformutil.Format(buildOpt.Platforms)
85-
// only specify a platform to pull when a single platform is used
86-
if len(platforms) == 1 {
87-
platform := platforms[0]
88-
pullOpt.Platform = &platform
89-
}
90100
}
91101

92102
toPull[target] = pullOpt

0 commit comments

Comments
 (0)