Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BUILDKIT_SANDBOX_HOSTNAME build-arg #2373

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,35 @@ import (
const (
DefaultLocalNameContext = "context"
DefaultLocalNameDockerfile = "dockerfile"
keyTarget = "target"
keyFilename = "filename"
keyCacheFrom = "cache-from" // for registry only. deprecated in favor of keyCacheImports
keyCacheImports = "cache-imports" // JSON representation of []CacheOptionsEntry
keyCacheNS = "build-arg:BUILDKIT_CACHE_MOUNT_NS"
defaultDockerfileName = "Dockerfile"
dockerignoreFilename = ".dockerignore"
buildArgPrefix = "build-arg:"
labelPrefix = "label:"
keyNoCache = "no-cache"
keyTargetPlatform = "platform"
keyMultiPlatform = "multi-platform"
keyImageResolveMode = "image-resolve-mode"
keyGlobalAddHosts = "add-hosts"
keyForceNetwork = "force-network-mode"
keyOverrideCopyImage = "override-copy-image" // remove after CopyOp implemented
keyNameContext = "contextkey"
keyNameDockerfile = "dockerfilekey"
keyContextSubDir = "contextsubdir"
keyContextKeepGitDir = "build-arg:BUILDKIT_CONTEXT_KEEP_GIT_DIR"
keySyntax = "build-arg:BUILDKIT_SYNTAX"
keyMultiPlatformArg = "build-arg:BUILDKIT_MULTI_PLATFORM"
keyHostname = "hostname"

buildArgPrefix = "build-arg:"
labelPrefix = "label:"

keyTarget = "target"
keyFilename = "filename"
keyCacheFrom = "cache-from" // for registry only. deprecated in favor of keyCacheImports
keyCacheImports = "cache-imports" // JSON representation of []CacheOptionsEntry
keyContextSubDir = "contextsubdir"
keyForceNetwork = "force-network-mode"
keyGlobalAddHosts = "add-hosts"
keyHostname = "hostname"
keyImageResolveMode = "image-resolve-mode"
keyMultiPlatform = "multi-platform"
keyNameContext = "contextkey"
keyNameDockerfile = "dockerfilekey"
keyNoCache = "no-cache"
keyOverrideCopyImage = "override-copy-image" // remove after CopyOp implemented
keyTargetPlatform = "platform"

// Don't forget to update frontend documentation if you add
// a new build-arg: frontend/dockerfile/docs/syntax.md
keyCacheNSArg = "build-arg:BUILDKIT_CACHE_MOUNT_NS"
keyContextKeepGitDirArg = "build-arg:BUILDKIT_CONTEXT_KEEP_GIT_DIR"
keyHostnameArg = "build-arg:BUILDKIT_SANDBOX_HOSTNAME"
keyMultiPlatformArg = "build-arg:BUILDKIT_MULTI_PLATFORM"
keySyntaxArg = "build-arg:BUILDKIT_SYNTAX"
)

var httpPrefix = regexp.MustCompile(`^https?://`)
Expand Down Expand Up @@ -150,7 +156,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {

var buildContext *llb.State
isNotLocalContext := false
if st, ok := detectGitContext(opts[localNameContext], opts[keyContextKeepGitDir]); ok {
if st, ok := detectGitContext(opts[localNameContext], opts[keyContextKeepGitDirArg]); ok {
if !forceLocalDockerfile {
src = *st
}
Expand Down Expand Up @@ -346,11 +352,11 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
}

if _, ok := opts["cmdline"]; !ok {
if cmdline, ok := opts[keySyntax]; ok {
if cmdline, ok := opts[keySyntaxArg]; ok {
p := strings.SplitN(strings.TrimSpace(cmdline), " ", 2)
res, err := forwardGateway(ctx, c, p[0], cmdline)
if err != nil && len(errdefs.Sources(err)) == 0 {
return nil, errors.Wrapf(err, "failed with %s = %s", keySyntax, cmdline)
return nil, errors.Wrapf(err, "failed with %s = %s", keySyntaxArg, cmdline)
}
return res, err
} else if ref, cmdline, loc, ok := dockerfile2llb.DetectSyntax(bytes.NewBuffer(dtDockerfile)); ok {
Expand Down Expand Up @@ -391,6 +397,10 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
}
res := client.NewResult()

if v, ok := opts[keyHostnameArg]; ok && len(v) > 0 {
opts[keyHostname] = v
}

eg, ctx = errgroup.WithContext(ctx)

for i, tp := range targetPlatforms {
Expand All @@ -407,7 +417,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
MetaResolver: c,
BuildArgs: filter(opts, buildArgPrefix),
Labels: filter(opts, labelPrefix),
CacheIDNamespace: opts[keyCacheNS],
CacheIDNamespace: opts[keyCacheNSArg],
SessionID: c.BuildOpts().SessionID,
BuildContext: buildContext,
Excludes: excludes,
Expand Down
49 changes: 38 additions & 11 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5121,9 +5121,9 @@ func testDockefileCheckHostname(t *testing.T, sb integration.Sandbox) {
f := getFrontend(t, sb)
dockerfile := []byte(`
FROM busybox
RUN cat /etc/hosts | grep testtest
RUN echo $HOSTNAME | grep testtest
RUN echo $(hostname) | grep testtest
RUN cat /etc/hosts | grep foo
RUN echo $HOSTNAME | grep foo
RUN echo $(hostname) | grep foo
`)

dir, err := tmpdir(
Expand All @@ -5136,16 +5136,43 @@ RUN echo $(hostname) | grep testtest
require.NoError(t, err)
defer c.Close()

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
FrontendAttrs: map[string]string{
"hostname": "testtest",
cases := []struct {
name string
attrs map[string]string
}{
{
name: "meta",
attrs: map[string]string{
"hostname": "foo",
},
},
LocalDirs: map[string]string{
builder.DefaultLocalNameDockerfile: dir,
builder.DefaultLocalNameContext: dir,
{
name: "arg",
attrs: map[string]string{
"build-arg:BUILDKIT_SANDBOX_HOSTNAME": "foo",
},
},
}, nil)
require.NoError(t, err)
{
name: "meta and arg",
attrs: map[string]string{
"hostname": "bar",
"build-arg:BUILDKIT_SANDBOX_HOSTNAME": "foo",
},
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
FrontendAttrs: tt.attrs,
LocalDirs: map[string]string{
builder.DefaultLocalNameDockerfile: dir,
builder.DefaultLocalNameContext: dir,
},
}, nil)
require.NoError(t, err)
})
}
}

// moby/buildkit#2311
Expand Down
12 changes: 8 additions & 4 deletions frontend/dockerfile/docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ eot
RUN FOO=abc ash /app/script.sh
```





## Built-in build args

* `BUILDKIT_CACHE_MOUNT_NS=<string>` set optional cache ID namespace
* `BUILDKIT_CONTEXT_KEEP_GIT_DIR=<bool>` trigger git context to keep the `.git` directory
* `BUILDKIT_INLINE_CACHE=<bool>` inline cache metadata to image configuration or not (for Docker-integrated BuildKit (`DOCKER_BUILDKIT=1 docker build`) and `docker buildx`)
* `BUILDKIT_MULTI_PLATFORM=<bool>` opt into determnistic output regardless of multi-platform output or not
* `BUILDKIT_SANDBOX_HOSTNAME=<string>` set the hostname (default `buildkitsandbox`)
* `BUILDKIT_SYNTAX=<image>` set frontend image