Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 0060d37

Browse files
committed
docker build: check experimental --platform on pre-run
Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit a88a1bea2360506a794d71f2be2a7477f8b4ebbb) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: c44c18e088d669612ff724e3c3212eee00a18d0d Component: cli
1 parent 86b614a commit 0060d37

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

components/cli/cli/command/image/build.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
115115
},
116116
}
117117

118+
// Wrap the global pre-run to handle non-BuildKit use of the --platform flag.
119+
//
120+
// We're doing it here so that we're only contacting the daemon when actually
121+
// running the command, and not during initialization.
122+
// TODO remove this hack once we no longer support the experimental use of --platform
123+
rootFn := cmd.Root().PersistentPreRunE
124+
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
125+
if ok, _ := command.BuildKitEnabled(dockerCli.ServerInfo()); !ok {
126+
f := cmd.Flag("platform")
127+
delete(f.Annotations, "buildkit")
128+
f.Annotations["version"] = []string{"1.32"}
129+
f.Annotations["experimental"] = nil
130+
}
131+
if rootFn != nil {
132+
return rootFn(cmd, args)
133+
}
134+
return nil
135+
}
136+
118137
flags := cmd.Flags()
119138

120139
flags.VarP(&options.tags, "tag", "t", "Name and optionally a tag in the 'name:tag' format")
@@ -163,14 +182,8 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
163182
command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled())
164183

165184
flags.StringVar(&options.platform, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable")
166-
// Platform is not experimental when BuildKit is used
167-
buildkitEnabled, err := command.BuildKitEnabled(dockerCli.ServerInfo())
168-
if err == nil && buildkitEnabled {
169-
flags.SetAnnotation("platform", "version", []string{"1.38"})
170-
} else {
171-
flags.SetAnnotation("platform", "version", []string{"1.32"})
172-
flags.SetAnnotation("platform", "experimental", nil)
173-
}
185+
flags.SetAnnotation("platform", "version", []string{"1.38"})
186+
flags.SetAnnotation("platform", "buildkit", nil)
174187

175188
flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
176189
flags.SetAnnotation("squash", "experimental", nil)

components/cli/cli/command/image/build_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import (
2020
"github.com/google/go-cmp/cmp"
2121
"github.com/moby/buildkit/session/secrets/secretsprovider"
2222
"gotest.tools/v3/assert"
23+
"gotest.tools/v3/env"
2324
"gotest.tools/v3/fs"
2425
"gotest.tools/v3/skip"
2526
)
2627

2728
func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) {
29+
defer env.Patch(t, "DOCKER_BUILDKIT", "0")()
2830
buffer := new(bytes.Buffer)
2931
fakeBuild := newFakeBuild()
3032
fakeImageBuild := func(ctx context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
@@ -60,6 +62,7 @@ func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) {
6062
}
6163

6264
func TestRunBuildResetsUidAndGidInContext(t *testing.T) {
65+
defer env.Patch(t, "DOCKER_BUILDKIT", "0")()
6366
skip.If(t, os.Getuid() != 0, "root is required to chown files")
6467
fakeBuild := newFakeBuild()
6568
cli := test.NewFakeCli(&fakeClient{imageBuildFunc: fakeBuild.build})
@@ -90,6 +93,7 @@ func TestRunBuildResetsUidAndGidInContext(t *testing.T) {
9093
}
9194

9295
func TestRunBuildDockerfileOutsideContext(t *testing.T) {
96+
defer env.Patch(t, "DOCKER_BUILDKIT", "0")()
9397
dir := fs.NewDir(t, t.Name(),
9498
fs.WithFile("data", "data file"))
9599
defer dir.Remove()
@@ -122,7 +126,8 @@ COPY data /data
122126
// TODO: test "context selection" logic directly when runBuild is refactored
123127
// to support testing (ex: docker/cli#294)
124128
func TestRunBuildFromGitHubSpecialCase(t *testing.T) {
125-
cmd := NewBuildCommand(test.NewFakeCli(nil))
129+
defer env.Patch(t, "DOCKER_BUILDKIT", "0")()
130+
cmd := NewBuildCommand(test.NewFakeCli(&fakeClient{}))
126131
// Clone a small repo that exists so git doesn't prompt for credentials
127132
cmd.SetArgs([]string{"github.com/docker/for-win"})
128133
cmd.SetOutput(ioutil.Discard)
@@ -135,6 +140,7 @@ func TestRunBuildFromGitHubSpecialCase(t *testing.T) {
135140
// starting with `github.com` takes precedence over the `github.com` special
136141
// case.
137142
func TestRunBuildFromLocalGitHubDir(t *testing.T) {
143+
defer env.Patch(t, "DOCKER_BUILDKIT", "0")()
138144
tmpDir, err := ioutil.TempDir("", "docker-build-from-local-dir-")
139145
assert.NilError(t, err)
140146
defer os.RemoveAll(tmpDir)
@@ -154,6 +160,7 @@ func TestRunBuildFromLocalGitHubDir(t *testing.T) {
154160
}
155161

156162
func TestRunBuildWithSymlinkedContext(t *testing.T) {
163+
defer env.Patch(t, "DOCKER_BUILDKIT", "0")()
157164
dockerfile := `
158165
FROM alpine:3.6
159166
RUN echo hello world

0 commit comments

Comments
 (0)