@@ -79,28 +79,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
7979 policy = types .IncludeDependencies
8080 }
8181
82- serviceDeps := false
83- project , err := project .WithServicesTransform (func (serviceName string , service types.ServiceConfig ) (types.ServiceConfig , error ) {
84- if service .Build != nil {
85- for _ , c := range service .Build .AdditionalContexts {
86- if t , found := strings .CutPrefix (c , types .ServicePrefix ); found {
87- serviceDeps = true
88- if service .DependsOn == nil {
89- service .DependsOn = map [string ]types.ServiceDependency {}
90- }
91- service .DependsOn [t ] = types.ServiceDependency {
92- Condition : "build" , // non-canonical, but will force dependency graph ordering
93- }
94- }
95- }
96- }
97- return service , nil
98- })
99- if err != nil {
100- return imageIDs , err
101- }
102-
103- err = project .ForEachService (options .Services , func (serviceName string , service * types.ServiceConfig ) error {
82+ err := project .ForEachService (options .Services , func (serviceName string , service * types.ServiceConfig ) error {
10483 if service .Build == nil {
10584 return nil
10685 }
@@ -124,8 +103,24 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
124103 return s .doBuildBake (ctx , project , serviceToBuild , options )
125104 }
126105
127- if serviceDeps {
128- logrus .Infof (`additional_context with "service:"" is better supported when delegating build go bake. Set COMPOSE_BAKE=true` )
106+ // Not using bake, additional_context: service:xx is implemented by building images in dependency order
107+ project , err = project .WithServicesTransform (func (serviceName string , service types.ServiceConfig ) (types.ServiceConfig , error ) {
108+ if service .Build != nil {
109+ for _ , c := range service .Build .AdditionalContexts {
110+ if t , found := strings .CutPrefix (c , types .ServicePrefix ); found {
111+ if service .DependsOn == nil {
112+ service .DependsOn = map [string ]types.ServiceDependency {}
113+ }
114+ service .DependsOn [t ] = types.ServiceDependency {
115+ Condition : "build" , // non-canonical, but will force dependency graph ordering
116+ }
117+ }
118+ }
119+ }
120+ return service , nil
121+ })
122+ if err != nil {
123+ return imageIDs , err
129124 }
130125
131126 // Initialize buildkit nodes
@@ -468,7 +463,7 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
468463 ContextPath : service .Build .Context ,
469464 DockerfileInline : service .Build .DockerfileInline ,
470465 DockerfilePath : dockerFilePath (service .Build .Context , service .Build .Dockerfile ),
471- NamedContexts : toBuildContexts (service . Build . AdditionalContexts ),
466+ NamedContexts : toBuildContexts (service , project ),
472467 },
473468 CacheFrom : pb .CreateCaches (cacheFrom .ToPB ()),
474469 CacheTo : pb .CreateCaches (cacheTo .ToPB ()),
@@ -573,13 +568,15 @@ func getImageBuildLabels(project *types.Project, service types.ServiceConfig) ty
573568 return ret
574569}
575570
576- func toBuildContexts (additionalContexts types.Mapping ) map [string ]build.NamedContext {
571+ func toBuildContexts (service types.ServiceConfig , project * types. Project ) map [string ]build.NamedContext {
577572 namedContexts := map [string ]build.NamedContext {}
578- for name , contextPath := range additionalContexts {
579- if _ , found := strings .CutPrefix (contextPath , types .ServicePrefix ); found {
580- // image we depend on has been build previously, as we run in dependency order.
581- // this assumes use of docker engine builder, so that build can access local images
582- continue
573+ for name , contextPath := range service .Build .AdditionalContexts {
574+ if strings .HasPrefix (contextPath , types .ServicePrefix ) {
575+ // image we depend on has been built previously, as we run in dependency order.
576+ // so we convert the service reference into an image reference
577+ target := contextPath [len (types .ServicePrefix ):]
578+ image := api .GetImageNameOrDefault (project .Services [target ], project .Name )
579+ contextPath = "docker-image://" + image
583580 }
584581 namedContexts [name ] = build.NamedContext {Path : contextPath }
585582 }
0 commit comments