Skip to content

Commit

Permalink
Merge pull request #863 from zachary-povey/support_host_network_in_co…
Browse files Browse the repository at this point in the history
…mpose

Add NetworkMode to bake target
  • Loading branch information
tonistiigi authored Dec 5, 2021
2 parents 62bdf4d + 7f8dbf8 commit 7371dda
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
21 changes: 15 additions & 6 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,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

// IMPORTANT: if you add more fields here, do not forget to update newOverrides and README.
}
Expand Down Expand Up @@ -501,6 +502,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...)
}

Expand Down Expand Up @@ -641,6 +645,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,
Expand All @@ -657,12 +665,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)
Expand Down
3 changes: 2 additions & 1 deletion bake/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions bake/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
build:
context: ./dir
dockerfile: Dockerfile-alternate
network:
none
args:
buildno: 123
`)
Expand All @@ -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) {
Expand Down

0 comments on commit 7371dda

Please sign in to comment.