Skip to content

Commit

Permalink
Add support for additional_contexts in build service config
Browse files Browse the repository at this point in the history
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
  • Loading branch information
laurazard committed Mar 13, 2023
1 parent fc4d2df commit 70f3d38
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
23 changes: 23 additions & 0 deletions e2e/cucumber-features/build-contexts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Feature: Build Contexts

Background:
Given a compose file
"""
services:
a:
build:
context: .
additional_contexts:
- dep=docker-image://ubuntu:latest
"""
And a dockerfile
"""
# syntax=docker/dockerfile:1
FROM alpine:latest
COPY --from=dep /etc/hostname /
"""

Scenario: Build w/ build context
When I run "compose build"
Then the exit code is 0

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/docker/compose/v2

go 1.20

replace github.com/compose-spec/compose-go => ../../../compose-spec/compose-go

require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/buger/goterm v1.0.4
Expand Down
11 changes: 11 additions & 0 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
if err != nil {
return err
}
if len(service.Build.AdditionalContexts) > 0 {
buildOptions.Inputs.NamedContexts = parseBuildContexts(service.Build.AdditionalContexts)
}
for _, image := range service.Build.CacheFrom {
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
Type: "registry",
Expand Down Expand Up @@ -402,6 +405,14 @@ func getImageBuildLabels(project *types.Project, service types.ServiceConfig) ty
return ret
}

func parseBuildContexts(additionalContexts map[string]*string) map[string]build.NamedContext {
namedContexts := map[string]build.NamedContext{}
for name, buildContext := range additionalContexts {
namedContexts[name] = build.NamedContext{Path: *buildContext}
}
return namedContexts
}

func useDockerDefaultPlatform(project *types.Project, platformList types.StringList) ([]specs.Platform, error) {
var plats []specs.Platform
if platform, ok := project.Environment["DOCKER_DEFAULT_PLATFORM"]; ok {
Expand Down
5 changes: 4 additions & 1 deletion pkg/compose/build_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
)

func (s *composeService) doBuildClassic(ctx context.Context, project *types.Project, opts map[string]buildx.Options) (map[string]string, error) {
var nameDigests = make(map[string]string)
nameDigests := make(map[string]string)
var errs error
err := project.WithServices(nil, func(service types.ServiceConfig) error {
imageName := api.GetImageNameOrDefault(service, project.Name)
Expand Down Expand Up @@ -103,6 +103,9 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
if utils.Contains(options.Allow, entitlements.EntitlementSecurityInsecure) {
return "", errors.Errorf("this builder doesn't support privileged mode, set DOCKER_BUILDKIT=1 to use builder supporting privileged mode")
}
if len(options.Inputs.NamedContexts) > 0 {
return "", errors.Errorf("this builder doesn't support additional contexts, set DOCKER_BUILDKIT=1 to use BuildKit which does")
}

if options.Labels == nil {
options.Labels = make(map[string]string)
Expand Down

0 comments on commit 70f3d38

Please sign in to comment.