Skip to content

Broken build-arg handling (requires =, ignores environment) #7028

Description

@andraxin

Contributing guidelines and issue reporting guide

Well-formed report checklist

  • I have found a bug that the documentation does not mention anything about my problem
  • I have found a bug that there are no open or closed issues that are related to my problem
  • I have provided version/information about my environment and done my best to provide a reproducer

Description of bug

Bug description

Passing --opt build-arg:IMAGE to buildctl build results in an error:

error: invalid opt: invalid value build-arg:IMAGE

even though the environment variable IMAGE is set.

Given that BuildKit's own documentation of build-arg handling is fairly sparse,
a reasonable expectation is that the original Docker/Buildx behavior would be followed.

The simple workaround of passing --opt build-arg:IMAGE=$IMAGE avoids the error,
but that should not be necessary.

Reproduction

$ docker run --rm -it --entrypoint sh --privileged -w /root moby/buildkit
~ # printf 'ARG IMAGE\nFROM $IMAGE\nRUN echo working\n' > Dockerfile
~ # export IMAGE=alpine
~ # buildctl-daemonless.sh build --frontend dockerfile.v0 --local dockerfile=. --opt build-arg:IMAGE
error: invalid opt: invalid value build-arg:IMAGE
~ # 
$ 

Version information

$ docker run --rm moby/buildkit --version
buildkitd github.com/moby/buildkit v0.32.2 991535e0973488b6a429096d21fa13f81f2d89d8
$

Additional notes

I tracked down the issue to BuildKit's unifrom handling of ALL --opt arguments,
via ParseOpt and attrMap in opt.go and build.go, respectively.

Contrast that with Buildx's listToMap function that optionally references environment variables:
https://github.com/docker/buildx/blob/556e63ede01ff0321cb5dda26826accfec9374f6/commands/build.go#L782-L800

As expected, build-arg processing turns that on:
https://github.com/docker/buildx/blob/556e63ede01ff0321cb5dda26826accfec9374f6/commands/build.go#L115

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions