From ae53101e896f5ae2348754f03156e31d3a0ac5af Mon Sep 17 00:00:00 2001 From: Zachary Povey Date: Thu, 25 Nov 2021 11:35:10 +0000 Subject: [PATCH] Add NetworkMode to bake target Allows specification of network mode in a bake target. Fixes #848 Signed-off-by: Zachary Povey --- bake/bake.go | 23 +++++++++++++++++------ bake/compose.go | 3 ++- bake/compose_test.go | 3 +++ bake/hcl_test.go | 4 ++++ docs/reference/buildx_bake.md | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index b6d4c6ee..3bd26f26 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -426,6 +426,7 @@ type Target struct { Outputs []string `json:"output,omitempty" hcl:"output,optional"` Pull *bool `json:"pull,omitempty" hcl:"pull,optional"` NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"` + NetworkMode *string `json:"network,omitempty" hcl:"network,optional"` // IMPORTANT: if you add more fields here, do not forget to update newOverrides and README. } @@ -492,6 +493,9 @@ func (t *Target) Merge(t2 *Target) { if t2.NoCache != nil { t.NoCache = t2.NoCache } + if t2.NetworkMode != nil { + t.NetworkMode = t2.NetworkMode + } t.Inherits = append(t.Inherits, t2.Inherits...) } @@ -563,6 +567,8 @@ func (t *Target) AddOverrides(overrides map[string]Override) error { } } } + case "network": + t.NetworkMode = &o.Value default: return errors.Errorf("unknown key: %s", keys[0]) } @@ -632,6 +638,10 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { if t.Pull != nil { pull = *t.Pull } + networkMode := "" + if t.NetworkMode != nil { + networkMode = *t.NetworkMode + } bi := build.Inputs{ ContextPath: contextPath, @@ -648,12 +658,13 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { t.Context = &bi.ContextPath bo := &build.Options{ - Inputs: bi, - Tags: t.Tags, - BuildArgs: t.Args, - Labels: t.Labels, - NoCache: noCache, - Pull: pull, + Inputs: bi, + Tags: t.Tags, + BuildArgs: t.Args, + Labels: t.Labels, + NoCache: noCache, + Pull: pull, + NetworkMode: networkMode, } platforms, err := platformutil.Parse(t.Platforms) diff --git a/bake/compose.go b/bake/compose.go index 48c71dbe..2ea88850 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -79,7 +79,8 @@ func ParseCompose(dt []byte) (*Config, error) { val, ok := cfg.Environment[val] return val, ok })), - CacheFrom: s.Build.CacheFrom, + CacheFrom: s.Build.CacheFrom, + NetworkMode: &s.Build.Network, } if err = t.composeExtTarget(s.Build.Extensions); err != nil { return nil, err diff --git a/bake/compose_test.go b/bake/compose_test.go index daa5f661..d9a4eb1a 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -19,6 +19,8 @@ services: build: context: ./dir dockerfile: Dockerfile-alternate + network: + none args: buildno: 123 `) @@ -43,6 +45,7 @@ services: require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile) require.Equal(t, 1, len(c.Targets[1].Args)) require.Equal(t, "123", c.Targets[1].Args["buildno"]) + require.Equal(t, "none", *c.Targets[1].NetworkMode) } func TestNoBuildOutOfTreeService(t *testing.T) { diff --git a/bake/hcl_test.go b/bake/hcl_test.go index 892f6bdd..53d25cdc 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -36,6 +36,7 @@ func TestHCLBasic(t *testing.T) { target "webapp-plus" { inherits = ["webapp", "cross"] + network = "host" args = { IAMCROSS = "true" } @@ -63,6 +64,7 @@ func TestHCLBasic(t *testing.T) { require.Equal(t, c.Targets[3].Name, "webapp-plus") require.Equal(t, 1, len(c.Targets[3].Args)) require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args) + require.Equal(t, "host", *c.Targets[3].NetworkMode) } func TestHCLBasicInJSON(t *testing.T) { @@ -93,6 +95,7 @@ func TestHCLBasicInJSON(t *testing.T) { }, "webapp-plus": { "inherits": ["webapp", "cross"], + "network": "host", "args": { "IAMCROSS": "true" } @@ -123,6 +126,7 @@ func TestHCLBasicInJSON(t *testing.T) { require.Equal(t, c.Targets[3].Name, "webapp-plus") require.Equal(t, 1, len(c.Targets[3].Args)) require.Equal(t, map[string]string{"IAMCROSS": "true"}, c.Targets[3].Args) + require.Equal(t, "host", *c.Targets[3].NetworkMode) } func TestHCLWithFunctions(t *testing.T) { diff --git a/docs/reference/buildx_bake.md b/docs/reference/buildx_bake.md index 16073ec5..cbd5f754 100644 --- a/docs/reference/buildx_bake.md +++ b/docs/reference/buildx_bake.md @@ -291,7 +291,7 @@ $ docker buildx bake --set foo*.no-cache # bypass caching only for Complete list of overridable fields: `args`, `cache-from`, `cache-to`, `context`, `dockerfile`, `labels`, `no-cache`, -`output`, `platform`, `pull`, `secrets`, `ssh`, `tags`, `target` +`output`, `platform`, `pull`, `secrets`, `ssh`, `tags`, `target`, `network` ### File definition