Skip to content

Commit 77ab539

Browse files
committed
allow additional_context to target another service
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 4cb0d38 commit 77ab539

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

loader/validate.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
// checkConsistency validate a compose model is consistent
3030
func checkConsistency(project *types.Project) error {
31-
for _, s := range project.Services {
31+
for name, s := range project.Services {
3232
if s.Build == nil && s.Image == "" {
3333
return fmt.Errorf("service %q has neither an image nor a build context specified: %w", s.Name, errdefs.ErrInvalid)
3434
}
@@ -38,6 +38,18 @@ func checkConsistency(project *types.Project) error {
3838
return fmt.Errorf("service %q declares mutualy exclusive dockerfile and dockerfile_inline: %w", s.Name, errdefs.ErrInvalid)
3939
}
4040

41+
for add, c := range s.Build.AdditionalContexts {
42+
if target, ok := strings.CutPrefix(c, types.ServicePrefix); ok {
43+
t, err := project.GetService(target)
44+
if err != nil {
45+
return fmt.Errorf("service %q declares unknown service %q as additional contexts %s", name, target, add)
46+
}
47+
if t.Build == nil {
48+
return fmt.Errorf("service %q declares non-buildable service %q as additional contexts %s", name, target, add)
49+
}
50+
}
51+
}
52+
4153
if len(s.Build.Platforms) > 0 && s.Platform != "" {
4254
var found bool
4355
for _, platform := range s.Build.Platforms {

paths/context.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616

1717
package paths
1818

19-
import "strings"
19+
import (
20+
"strings"
21+
22+
"github.com/compose-spec/compose-go/v2/types"
23+
)
2024

2125
func (r *relativePathsResolver) absContextPath(value any) (any, error) {
2226
v := value.(string)
2327
if strings.Contains(v, "://") { // `docker-image://` or any builder specific context type
2428
return v, nil
2529
}
30+
if strings.HasPrefix(v, types.ServicePrefix) { // `docker-image://` or any builder specific context type
31+
return v, nil
32+
}
2633
if isRemoteContext(v) {
2734
return v, nil
2835
}

0 commit comments

Comments
 (0)