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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-06 13:46:51 +01:00
parent 65d3f7830d
commit 60d62fb729
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 24 additions and 26 deletions

View File

@ -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
}

View File

@ -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"),
})
}

View File

@ -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"),