From 60d62fb7297f5272b7ffad2c3e5c7b6056bcccff Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 6 Dec 2022 13:46:51 +0100 Subject: [PATCH] cmd/docker: improve error message if BUILDKIT_ENABLED=0 Before this change, the error would suggest installing buildx: echo "FROM scratch" | DOCKER_BUILDKIT=0 docker build - DEPRECATED: The legacy builder is deprecated and will be removed in a future release. Install the buildx component to build images with BuildKit: https://docs.docker.com/go/buildx/ ... However, this error would also be shown if buildx is actually installed, but disabled through "DOCKER_BUILDKIT=0"; docker buildx version github.com/docker/buildx v0.9.1 ed00243 With this patch, it reports that it's disabled, and how to fix: echo "FROM scratch" | DOCKER_BUILDKIT=0 docker build - DEPRECATED: The legacy builder is deprecated and will be removed in a future release. BuildKit is currently disabled; enabled it by removing the DOCKER_BUILDKIT=0 environment-variable. Signed-off-by: Sebastiaan van Stijn --- cmd/docker/builder.go | 41 ++++++++++++++++++-------------------- cmd/docker/builder_test.go | 1 + e2e/image/build_test.go | 8 ++++---- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/cmd/docker/builder.go b/cmd/docker/builder.go index 7d463e4a76..1eab9fc776 100644 --- a/cmd/docker/builder.go +++ b/cmd/docker/builder.go @@ -18,34 +18,30 @@ const ( builderDefaultPlugin = "buildx" buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. Install the buildx component to build images with BuildKit: - https://docs.docker.com/go/buildx/ -` + https://docs.docker.com/go/buildx/` + + buildkitDisabledWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. + BuildKit is currently disabled; enabled it by removing the DOCKER_BUILDKIT=0 + environment-variable.` buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken. Install the buildx component to build images with BuildKit: - https://docs.docker.com/go/buildx/ -` + https://docs.docker.com/go/buildx/` ) -func newBuilderError(warn bool, err error) error { - var errorMsg string - if warn { - errorMsg = buildxMissingWarning - } else { - errorMsg = buildxMissingError - } - if pluginmanager.IsNotFound(err) { +func newBuilderError(errorMsg string, pluginLoadErr error) error { + if pluginmanager.IsNotFound(pluginLoadErr) { return errors.New(errorMsg) } - if err != nil { - return fmt.Errorf("%w\n\n%s", err, errorMsg) + if pluginLoadErr != nil { + return fmt.Errorf("%w\n\n%s", pluginLoadErr, errorMsg) } - return fmt.Errorf("%s", errorMsg) + return errors.New(errorMsg) } //nolint:gocyclo func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []string) ([]string, []string, []string, error) { - var useLegacy, useBuilder, useAlias bool + var buildKitDisabled, useBuilder, useAlias bool var envs []string // check DOCKER_BUILDKIT env var is present and @@ -56,7 +52,7 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st return args, osargs, nil, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value") } if !enabled { - useLegacy = true + buildKitDisabled = true } else { useBuilder = true } @@ -84,10 +80,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st return args, osargs, nil, nil } - if useLegacy { + if buildKitDisabled { // display warning if not wcow and continue if dockerCli.ServerInfo().OSType != "windows" { - _, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, nil)) + _, _ = fmt.Fprintf(dockerCli.Err(), "%s\n\n", buildkitDisabledWarning) } return args, osargs, nil, nil } @@ -98,12 +94,13 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st perr = plugin.Err } if perr != nil { - // if builder enforced with DOCKER_BUILDKIT=1, cmd must fail if plugin missing or broken + // if builder is enforced with DOCKER_BUILDKIT=1, cmd must fail + // if the plugin is missing or broken. if useBuilder { - return args, osargs, nil, newBuilderError(false, perr) + return args, osargs, nil, newBuilderError(buildxMissingError, perr) } // otherwise, display warning and continue - _, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, perr)) + _, _ = fmt.Fprintf(dockerCli.Err(), "%s\n\n", newBuilderError(buildxMissingWarning, perr)) return args, osargs, nil, nil } diff --git a/cmd/docker/builder_test.go b/cmd/docker/builder_test.go index 9e7d82ee86..f31c1d8fb7 100644 --- a/cmd/docker/builder_test.go +++ b/cmd/docker/builder_test.go @@ -155,6 +155,7 @@ func TestBuildkitDisabled(t *testing.T) { output.Assert(t, b.String(), map[int]func(string) error{ 0: output.Suffix("DEPRECATED: The legacy builder is deprecated and will be removed in a future release."), + 1: output.Suffix("BuildKit is currently disabled; enabled it by removing the DOCKER_BUILDKIT=0"), }) } diff --git a/e2e/image/build_test.go b/e2e/image/build_test.go index a66965c5a2..5d7c99a8e7 100644 --- a/e2e/image/build_test.go +++ b/e2e/image/build_test.go @@ -36,12 +36,12 @@ func TestBuildFromContextDirectoryWithTag(t *testing.T) { withWorkingDir(dir)) defer icmd.RunCommand("docker", "image", "rm", "myimage") - const buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. - Install the buildx component to build images with BuildKit: - https://docs.docker.com/go/buildx/ + const buildkitDisabledWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release. + BuildKit is currently disabled; enabled it by removing the DOCKER_BUILDKIT=0 + environment-variable. ` - result.Assert(t, icmd.Expected{Err: buildxMissingWarning}) + result.Assert(t, icmd.Expected{Err: buildkitDisabledWarning}) output.Assert(t, result.Stdout(), map[int]func(string) error{ 0: output.Prefix("Sending build context to Docker daemon"), 1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:3.6"),