Skip to content

Commit 200f47e

Browse files
laurazardndeloof
authored andcommitted
Add support for additional_contexts in build service config
Signed-off-by: Laura Brehm <[email protected]>
1 parent e0aaccf commit 200f47e

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Feature: Build Contexts
2+
3+
Background:
4+
Given a compose file
5+
"""
6+
services:
7+
a:
8+
build:
9+
context: .
10+
additional_contexts:
11+
- dep=docker-image://ubuntu:latest
12+
"""
13+
And a dockerfile
14+
"""
15+
# syntax=docker/dockerfile:1
16+
FROM alpine:latest
17+
COPY --from=dep /etc/hostname /
18+
"""
19+
20+
Scenario: Build w/ build context
21+
When I run "compose build"
22+
Then the exit code is 0
23+

pkg/compose/build.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
8080
if err != nil {
8181
return err
8282
}
83+
if len(service.Build.AdditionalContexts) > 0 {
84+
buildOptions.Inputs.NamedContexts = toBuildContexts(service.Build.AdditionalContexts)
85+
}
8386
for _, image := range service.Build.CacheFrom {
8487
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
8588
Type: "registry",
@@ -200,7 +203,6 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
200203
}
201204
}
202205
return opts, nil
203-
204206
}
205207

206208
func (s *composeService) getLocalImagesDigests(ctx context.Context, project *types.Project) (map[string]string, error) {
@@ -422,6 +424,14 @@ func getImageBuildLabels(project *types.Project, service types.ServiceConfig) ty
422424
return ret
423425
}
424426

427+
func toBuildContexts(additionalContexts map[string]*string) map[string]build.NamedContext {
428+
namedContexts := map[string]build.NamedContext{}
429+
for name, buildContext := range additionalContexts {
430+
namedContexts[name] = build.NamedContext{Path: *buildContext}
431+
}
432+
return namedContexts
433+
}
434+
425435
func useDockerDefaultPlatform(project *types.Project, platformList types.StringList) ([]specs.Platform, error) {
426436
var plats []specs.Platform
427437
if platform, ok := project.Environment["DOCKER_DEFAULT_PLATFORM"]; ok {

pkg/compose/build_classic.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646
)
4747

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

107110
if options.Labels == nil {
108111
options.Labels = make(map[string]string)

0 commit comments

Comments
 (0)