Add NetworkMode to bake target

Allows specification of network mode in a bake target.

Fixes #848

Signed-off-by: Zachary Povey <zachary.povey@autotrader.co.uk>
pull/863/head
Zachary Povey 3 years ago
parent ab73275f58
commit ae53101e89

@ -426,6 +426,7 @@ type Target struct {
Outputs []string `json:"output,omitempty" hcl:"output,optional"` Outputs []string `json:"output,omitempty" hcl:"output,optional"`
Pull *bool `json:"pull,omitempty" hcl:"pull,optional"` Pull *bool `json:"pull,omitempty" hcl:"pull,optional"`
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,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. // 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 { if t2.NoCache != nil {
t.NoCache = t2.NoCache t.NoCache = t2.NoCache
} }
if t2.NetworkMode != nil {
t.NetworkMode = t2.NetworkMode
}
t.Inherits = append(t.Inherits, t2.Inherits...) 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: default:
return errors.Errorf("unknown key: %s", keys[0]) 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 { if t.Pull != nil {
pull = *t.Pull pull = *t.Pull
} }
networkMode := ""
if t.NetworkMode != nil {
networkMode = *t.NetworkMode
}
bi := build.Inputs{ bi := build.Inputs{
ContextPath: contextPath, ContextPath: contextPath,
@ -654,6 +664,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
Labels: t.Labels, Labels: t.Labels,
NoCache: noCache, NoCache: noCache,
Pull: pull, Pull: pull,
NetworkMode: networkMode,
} }
platforms, err := platformutil.Parse(t.Platforms) platforms, err := platformutil.Parse(t.Platforms)

@ -80,6 +80,7 @@ func ParseCompose(dt []byte) (*Config, error) {
return val, ok return val, ok
})), })),
CacheFrom: s.Build.CacheFrom, CacheFrom: s.Build.CacheFrom,
NetworkMode: &s.Build.Network,
} }
if err = t.composeExtTarget(s.Build.Extensions); err != nil { if err = t.composeExtTarget(s.Build.Extensions); err != nil {
return nil, err return nil, err

@ -19,6 +19,8 @@ services:
build: build:
context: ./dir context: ./dir
dockerfile: Dockerfile-alternate dockerfile: Dockerfile-alternate
network:
none
args: args:
buildno: 123 buildno: 123
`) `)
@ -43,6 +45,7 @@ services:
require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile) require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile)
require.Equal(t, 1, len(c.Targets[1].Args)) require.Equal(t, 1, len(c.Targets[1].Args))
require.Equal(t, "123", c.Targets[1].Args["buildno"]) require.Equal(t, "123", c.Targets[1].Args["buildno"])
require.Equal(t, "none", *c.Targets[1].NetworkMode)
} }
func TestNoBuildOutOfTreeService(t *testing.T) { func TestNoBuildOutOfTreeService(t *testing.T) {

@ -36,6 +36,7 @@ func TestHCLBasic(t *testing.T) {
target "webapp-plus" { target "webapp-plus" {
inherits = ["webapp", "cross"] inherits = ["webapp", "cross"]
network = "host"
args = { args = {
IAMCROSS = "true" IAMCROSS = "true"
} }
@ -63,6 +64,7 @@ func TestHCLBasic(t *testing.T) {
require.Equal(t, c.Targets[3].Name, "webapp-plus") require.Equal(t, c.Targets[3].Name, "webapp-plus")
require.Equal(t, 1, len(c.Targets[3].Args)) require.Equal(t, 1, len(c.Targets[3].Args))
require.Equal(t, map[string]string{"IAMCROSS": "true"}, 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) { func TestHCLBasicInJSON(t *testing.T) {
@ -93,6 +95,7 @@ func TestHCLBasicInJSON(t *testing.T) {
}, },
"webapp-plus": { "webapp-plus": {
"inherits": ["webapp", "cross"], "inherits": ["webapp", "cross"],
"network": "host",
"args": { "args": {
"IAMCROSS": "true" "IAMCROSS": "true"
} }
@ -123,6 +126,7 @@ func TestHCLBasicInJSON(t *testing.T) {
require.Equal(t, c.Targets[3].Name, "webapp-plus") require.Equal(t, c.Targets[3].Name, "webapp-plus")
require.Equal(t, 1, len(c.Targets[3].Args)) require.Equal(t, 1, len(c.Targets[3].Args))
require.Equal(t, map[string]string{"IAMCROSS": "true"}, 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) { func TestHCLWithFunctions(t *testing.T) {

@ -291,7 +291,7 @@ $ docker buildx bake --set foo*.no-cache # bypass caching only for
Complete list of overridable fields: Complete list of overridable fields:
`args`, `cache-from`, `cache-to`, `context`, `dockerfile`, `labels`, `no-cache`, `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 ### File definition

Loading…
Cancel
Save