Skip to content

Commit

Permalink
Merge pull request docker#87 from tiborvass/no-build-field
Browse files Browse the repository at this point in the history
[Carry docker#79] Change compose file handling to require valid service specifications
  • Loading branch information
tonistiigi authored May 30, 2019
2 parents 8b6dfbd + b741350 commit ab5fe3d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bake/compose.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bake

import (
"fmt"
"reflect"

"github.com/docker/cli/cli/compose/loader"
composetypes "github.com/docker/cli/cli/compose/types"
)
Expand All @@ -26,14 +29,23 @@ func ParseCompose(dt []byte) (*Config, error) {
}

var c Config
var zeroBuildConfig composetypes.BuildConfig
if len(cfg.Services) > 0 {
c.Group = map[string]Group{}
c.Target = map[string]Target{}

var g Group

for _, s := range cfg.Services {
g.Targets = append(g.Targets, s.Name)

if reflect.DeepEqual(s.Build, zeroBuildConfig) {
// if not make sure they're setting an image or it's invalid d-c.yml
if s.Image == "" {
return nil, fmt.Errorf("compose file invalid: service %s has neither an image nor a build context specified. At least one must be provided.", s.Name)
}
continue
}

var contextPathP *string
if s.Build.Context != "" {
contextPath := s.Build.Context
Expand All @@ -44,6 +56,7 @@ func ParseCompose(dt []byte) (*Config, error) {
dockerfilePath := s.Build.Dockerfile
dockerfilePathP = &dockerfilePath
}
g.Targets = append(g.Targets, s.Name)
t := Target{
Context: contextPathP,
Dockerfile: dockerfilePathP,
Expand Down
56 changes: 56 additions & 0 deletions bake/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,33 @@ services:
require.Equal(t, "123", c.Target["webapp"].Args["buildno"])
}

func TestNoBuildOutOfTreeService(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
external:
image: "verycooldb:1337"
webapp:
build: ./db
`)
c, err := ParseCompose(dt)
require.NoError(t, err)
require.Equal(t, 1, len(c.Group))
}

func TestParseComposeTarget(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
db:
build:
context: ./db
target: db
webapp:
build:
context: .
target: webapp
`)

Expand All @@ -59,3 +76,42 @@ services:
require.Equal(t, "db", *c.Target["db"].Target)
require.Equal(t, "webapp", *c.Target["webapp"].Target)
}

func TestComposeBuildWithoutContext(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
db:
build:
target: db
webapp:
build:
context: .
target: webapp
`)

c, err := ParseCompose(dt)
require.NoError(t, err)
require.Equal(t, "db", *c.Target["db"].Target)
require.Equal(t, "webapp", *c.Target["webapp"].Target)
}

func TestBogusCompose(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
db:
labels:
- "foo"
webapp:
build:
context: .
target: webapp
`)

_, err := ParseCompose(dt)
require.Error(t, err)
require.Contains(t, err.Error(), "has neither an image nor a build context specified. At least one must be provided")
}
1 change: 1 addition & 0 deletions hack/demo-env/examples/compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
image: docker.io/tonistiigi/db
webapp:
build:
context: .
dockerfile: Dockerfile.webapp
args:
buildno: 1

0 comments on commit ab5fe3d

Please sign in to comment.