Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Use render to load docker-compose.yml with defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Nov 15, 2019
1 parent d72712d commit 02a383c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 153 deletions.
7 changes: 4 additions & 3 deletions internal/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
compose "github.com/docker/cli/cli/compose/types"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -194,17 +195,17 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
return bundle, nil
}

func fixServiceImageReferences(ctx context.Context, dockerCli command.Cli, bundle *bundle.Bundle, pulledServices []ServiceConfig) error {
func fixServiceImageReferences(ctx context.Context, dockerCli command.Cli, bundle *bundle.Bundle, pulledServices []compose.ServiceConfig) error {
insecureRegistries, err := internal.InsecureRegistriesFromEngine(dockerCli)
if err != nil {
return errors.Wrapf(err, "could not retrieve insecure registries")
}
resolver := remotes.CreateResolver(dockerCli.ConfigFile(), insecureRegistries...)
for _, service := range pulledServices {
image := bundle.Images[service.Name]
ref, err := reference.ParseDockerRef(*service.Image)
ref, err := reference.ParseDockerRef(service.Image)
if err != nil {
return errors.Wrapf(err, "could not resolve image %s", *service.Image)
return errors.Wrapf(err, "could not resolve image %s", service.Image)
}
_, desc, err := resolver.Resolve(ctx, ref.String())
if err != nil {
Expand Down
60 changes: 47 additions & 13 deletions internal/commands/build/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@ package build
import (
"fmt"
"path"
"path/filepath"
"strings"

"github.com/docker/app/render"

"github.com/docker/app/types"
"github.com/docker/buildx/build"
"github.com/docker/cli/cli/compose/loader"
compose "github.com/docker/cli/cli/compose/types"
)

// parseCompose do parse app compose file and extract buildx Options
// We don't rely on bake's ReadTargets + TargetsToBuildOpt here as we have to skip environment variable interpolation
func parseCompose(app *types.App, contextPath string, options buildOptions) (map[string]build.Options, []ServiceConfig, error) {
parsed, err := loader.ParseYAML(app.Composes()[0])
func parseCompose(app *types.App, contextPath string, options buildOptions) (map[string]build.Options, []compose.ServiceConfig, error) {
comp, err := render.Render(app, nil, nil)
if err != nil {
return nil, nil, err
}

services, err := load(parsed, options.args)
if err != nil {
return nil, nil, fmt.Errorf("Failed to parse compose file: %s", err)
}
buildArgs := buildArgsToMap(options.args)

pulledServices := []ServiceConfig{}
pulledServices := []compose.ServiceConfig{}
opts := map[string]build.Options{}
for _, service := range services {
if service.Build == nil {
for _, service := range comp.Services {
// Sanity check
for _, vol := range service.Volumes {
if vol.Type == "bind" && !filepath.IsAbs(vol.Source) {
return nil, nil, fmt.Errorf("invalid service %q: can't use relative path as volume source", service.Name)
}
}

if service.Build.Context == "" {
pulledServices = append(pulledServices, service)
continue
}
var tags []string
if service.Image != nil {
tags = append(tags, *service.Image)
if service.Image != "" {
tags = append(tags, service.Image)
}

if service.Build.Dockerfile == "" {
Expand All @@ -43,7 +50,7 @@ func parseCompose(app *types.App, contextPath string, options buildOptions) (map
ContextPath: path.Join(contextPath, service.Build.Context),
DockerfilePath: path.Join(contextPath, service.Build.Context, service.Build.Dockerfile),
},
BuildArgs: flatten(service.Build.Args),
BuildArgs: flatten(mergeArgs(service.Build.Args, buildArgs)),
NoCache: options.noCache,
Pull: options.pull,
Tags: tags,
Expand All @@ -52,6 +59,33 @@ func parseCompose(app *types.App, contextPath string, options buildOptions) (map
return opts, pulledServices, nil
}

func buildArgsToMap(array []string) map[string]string {
result := make(map[string]string)
for _, value := range array {
parts := strings.SplitN(value, "=", 2)
key := parts[0]
if len(parts) == 1 {
result[key] = ""
} else {
result[key] = parts[1]
}
}
return result
}

func mergeArgs(src compose.MappingWithEquals, values map[string]string) compose.MappingWithEquals {
for key := range src {
if val, ok := values[key]; ok {
if val == "" {
src[key] = nil
} else {
src[key] = &val
}
}
}
return src
}

func flatten(in compose.MappingWithEquals) map[string]string {
if len(in) == 0 {
return nil
Expand Down
137 changes: 0 additions & 137 deletions internal/commands/build/types.go

This file was deleted.

0 comments on commit 02a383c

Please sign in to comment.