2020-11-25 12:17:33 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-08 11:53:36 +01:00
|
|
|
package compose
|
2020-11-25 12:17:33 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-30 08:47:09 -04:00
|
|
|
"errors"
|
2020-11-25 16:51:54 +01:00
|
|
|
"fmt"
|
2023-05-08 15:21:41 +02:00
|
|
|
"os"
|
2025-01-22 14:41:52 +01:00
|
|
|
"strings"
|
2025-02-21 11:05:12 +01:00
|
|
|
"time"
|
2020-11-25 12:17:33 +01:00
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2024-07-17 10:34:02 +02:00
|
|
|
"github.com/containerd/platforms"
|
2020-11-25 12:17:33 +01:00
|
|
|
"github.com/docker/buildx/build"
|
2023-08-10 08:57:28 -04:00
|
|
|
"github.com/docker/buildx/builder"
|
|
|
|
"github.com/docker/buildx/controller/pb"
|
2023-01-10 10:18:09 +01:00
|
|
|
"github.com/docker/buildx/store/storeutil"
|
2021-05-06 15:42:08 +02:00
|
|
|
"github.com/docker/buildx/util/buildflags"
|
2021-06-15 09:57:38 +02:00
|
|
|
xprogress "github.com/docker/buildx/util/progress"
|
2023-08-10 08:57:28 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2023-11-13 19:10:29 +01:00
|
|
|
cliopts "github.com/docker/cli/opts"
|
2023-11-20 18:16:15 +01:00
|
|
|
"github.com/docker/compose/v2/internal/tracing"
|
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/progress"
|
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2024-06-07 17:57:54 +02:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2020-12-11 11:08:44 +01:00
|
|
|
bclient "github.com/moby/buildkit/client"
|
2021-05-06 15:42:08 +02:00
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/auth/authprovider"
|
2022-04-13 16:37:43 +02:00
|
|
|
"github.com/moby/buildkit/session/secrets/secretsprovider"
|
2022-03-30 11:47:34 +02:00
|
|
|
"github.com/moby/buildkit/session/sshforward/sshprovider"
|
2022-12-21 10:20:46 +01:00
|
|
|
"github.com/moby/buildkit/util/entitlements"
|
2024-06-07 17:57:54 +02:00
|
|
|
"github.com/moby/buildkit/util/progress/progressui"
|
2021-02-27 07:43:37 -03:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2023-06-14 10:20:36 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2025-02-26 10:50:22 +01:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-04-06 23:19:55 +02:00
|
|
|
|
2023-08-10 08:57:28 -04:00
|
|
|
// required to get default driver registered
|
|
|
|
_ "github.com/docker/buildx/driver/docker"
|
2020-11-25 12:17:33 +01:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (s *composeService) Build(ctx context.Context, project *types.Project, options api.BuildOptions) error {
|
2023-03-16 14:32:50 +01:00
|
|
|
err := options.Apply(project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-02 20:15:35 +02:00
|
|
|
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
|
2025-02-26 10:53:50 +01:00
|
|
|
return tracing.SpanWrapFunc("project/build", tracing.ProjectOptions(ctx, project),
|
|
|
|
func(ctx context.Context) error {
|
|
|
|
_, err := s.build(ctx, project, options, nil)
|
|
|
|
return err
|
|
|
|
})(ctx)
|
2023-05-11 18:45:00 +02:00
|
|
|
}, s.stdinfo(), "Building")
|
2021-06-03 13:57:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 08:57:28 -04:00
|
|
|
//nolint:gocyclo
|
2025-02-21 11:05:12 +01:00
|
|
|
func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions, localImages map[string]api.ImageSummary) (map[string]string, error) {
|
2023-11-20 18:16:15 +01:00
|
|
|
imageIDs := map[string]string{}
|
2024-02-09 11:40:29 +01:00
|
|
|
serviceToBuild := types.Services{}
|
2023-12-20 15:18:29 +01:00
|
|
|
|
|
|
|
var policy types.DependencyOption = types.IgnoreDependencies
|
|
|
|
if options.Deps {
|
|
|
|
policy = types.IncludeDependencies
|
|
|
|
}
|
2025-01-22 14:41:52 +01:00
|
|
|
|
2025-05-13 09:54:47 +02:00
|
|
|
var err error
|
2025-04-28 08:59:07 +02:00
|
|
|
if len(options.Services) > 0 {
|
|
|
|
// As user requested some services to be built, also include those used as additional_contexts
|
|
|
|
options.Services = addBuildDependencies(options.Services, project)
|
2025-05-13 09:54:47 +02:00
|
|
|
// Some build dependencies we just introduced may not be enabled
|
|
|
|
project, err = project.WithServicesEnabled(options.Services...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-04-28 08:59:07 +02:00
|
|
|
}
|
2025-05-13 09:54:47 +02:00
|
|
|
project, err = project.WithSelectedServices(options.Services)
|
2025-04-28 08:59:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = project.ForEachService(options.Services, func(serviceName string, service *types.ServiceConfig) error {
|
2023-11-20 18:16:15 +01:00
|
|
|
if service.Build == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-29 11:45:45 +01:00
|
|
|
image := api.GetImageNameOrDefault(*service, project.Name)
|
2023-11-20 18:16:15 +01:00
|
|
|
_, localImagePresent := localImages[image]
|
|
|
|
if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-09 11:40:29 +01:00
|
|
|
serviceToBuild[serviceName] = *service
|
2023-11-20 18:16:15 +01:00
|
|
|
return nil
|
2023-12-20 15:18:29 +01:00
|
|
|
}, policy)
|
2024-02-09 11:40:29 +01:00
|
|
|
if err != nil || len(serviceToBuild) == 0 {
|
2023-11-20 18:16:15 +01:00
|
|
|
return imageIDs, err
|
|
|
|
}
|
|
|
|
|
2024-02-09 11:40:29 +01:00
|
|
|
bake, err := buildWithBake(s.dockerCli)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-03-26 11:23:42 +01:00
|
|
|
if bake || options.Print {
|
2025-02-26 10:50:22 +01:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "bake"))
|
2024-02-09 11:40:29 +01:00
|
|
|
return s.doBuildBake(ctx, project, serviceToBuild, options)
|
|
|
|
}
|
|
|
|
|
2025-02-25 09:16:14 +01:00
|
|
|
// Not using bake, additional_context: service:xx is implemented by building images in dependency order
|
|
|
|
project, err = project.WithServicesTransform(func(serviceName string, service types.ServiceConfig) (types.ServiceConfig, error) {
|
|
|
|
if service.Build != nil {
|
|
|
|
for _, c := range service.Build.AdditionalContexts {
|
|
|
|
if t, found := strings.CutPrefix(c, types.ServicePrefix); found {
|
|
|
|
if service.DependsOn == nil {
|
|
|
|
service.DependsOn = map[string]types.ServiceDependency{}
|
|
|
|
}
|
|
|
|
service.DependsOn[t] = types.ServiceDependency{
|
|
|
|
Condition: "build", // non-canonical, but will force dependency graph ordering
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return service, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return imageIDs, err
|
2025-02-22 01:25:27 +01:00
|
|
|
}
|
|
|
|
|
2023-08-03 21:11:16 +02:00
|
|
|
// Initialize buildkit nodes
|
2024-02-09 11:40:29 +01:00
|
|
|
buildkitEnabled, err := s.dockerCli.BuildKitEnabled()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 21:11:16 +02:00
|
|
|
var (
|
|
|
|
b *builder.Builder
|
|
|
|
nodes []builder.Node
|
2023-07-24 16:11:06 +02:00
|
|
|
w *xprogress.Printer
|
2023-08-03 21:11:16 +02:00
|
|
|
)
|
|
|
|
if buildkitEnabled {
|
|
|
|
builderName := options.Builder
|
|
|
|
if builderName == "" {
|
|
|
|
builderName = os.Getenv("BUILDX_BUILDER")
|
|
|
|
}
|
|
|
|
b, err = builder.New(s.dockerCli, builder.WithName(builderName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-22 18:52:32 +01:00
|
|
|
nodes, err = b.LoadNodes(ctx)
|
2023-08-03 21:11:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:11:06 +02:00
|
|
|
// Progress needs its own context that lives longer than the
|
|
|
|
// build one otherwise it won't read all the messages from
|
|
|
|
// build and will lock
|
|
|
|
progressCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-05-25 15:33:43 +02:00
|
|
|
|
2024-02-19 15:38:45 +01:00
|
|
|
if options.Quiet {
|
|
|
|
options.Progress = progress.ModeQuiet
|
|
|
|
}
|
2025-01-13 14:03:03 +01:00
|
|
|
if options.Progress == "" {
|
|
|
|
options.Progress = os.Getenv("BUILDKIT_PROGRESS")
|
|
|
|
}
|
2023-11-22 18:52:32 +01:00
|
|
|
w, err = xprogress.NewPrinter(progressCtx, os.Stdout, progressui.DisplayMode(options.Progress),
|
2023-07-24 16:11:06 +02:00
|
|
|
xprogress.WithDesc(
|
|
|
|
fmt.Sprintf("building with %q instance using %s driver", b.Name, b.Driver),
|
|
|
|
fmt.Sprintf("%s:%s", b.Driver, b.Name),
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-08 15:21:41 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 10:14:31 +01:00
|
|
|
// we use a pre-allocated []string to collect build digest by service index while running concurrent goroutines
|
2023-07-17 10:36:48 -04:00
|
|
|
builtDigests := make([]string, len(project.Services))
|
2023-11-27 10:14:31 +01:00
|
|
|
names := project.ServiceNames()
|
|
|
|
getServiceIndex := func(name string) int {
|
|
|
|
for idx, n := range names {
|
|
|
|
if n == name {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
2025-01-08 15:55:05 +01:00
|
|
|
|
|
|
|
cw := progress.ContextWriter(ctx)
|
2023-03-16 14:32:50 +01:00
|
|
|
err = InDependencyOrder(ctx, project, func(ctx context.Context, name string) error {
|
2024-02-09 11:40:29 +01:00
|
|
|
service, ok := serviceToBuild[name]
|
2023-11-20 18:16:15 +01:00
|
|
|
if !ok {
|
2023-08-30 08:47:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
2024-02-13 21:06:56 +01:00
|
|
|
serviceName := fmt.Sprintf("Service %s", name)
|
|
|
|
|
2023-05-08 15:21:41 +02:00
|
|
|
if !buildkitEnabled {
|
2025-02-26 10:50:22 +01:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "classic"))
|
2024-02-13 21:06:56 +01:00
|
|
|
cw.Event(progress.BuildingEvent(serviceName))
|
2023-08-10 08:57:28 -04:00
|
|
|
id, err := s.doBuildClassic(ctx, project, service, options)
|
2023-03-14 13:33:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-13 21:06:56 +01:00
|
|
|
cw.Event(progress.BuiltEvent(serviceName))
|
2023-11-27 10:14:31 +01:00
|
|
|
builtDigests[getServiceIndex(name)] = id
|
2023-03-16 14:32:50 +01:00
|
|
|
|
2023-05-08 15:21:41 +02:00
|
|
|
if options.Push {
|
|
|
|
return s.push(ctx, project, api.PushOptions{})
|
2023-03-14 13:33:33 +01:00
|
|
|
}
|
2023-03-16 14:32:50 +01:00
|
|
|
return nil
|
2022-08-08 16:03:36 +02:00
|
|
|
}
|
2023-05-08 15:21:41 +02:00
|
|
|
|
|
|
|
if options.Memory != 0 {
|
fix linting issues with golangci-lint 1.60.2
pkg/watch/watcher_darwin.go:96:16: Error return value of `d.stream.Start` is not checked (errcheck)
d.stream.Start()
^
pkg/prompt/prompt.go:97:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(u.stdout, message)
^
pkg/prompt/prompt.go:99:12: Error return value of `fmt.Scanln` is not checked (errcheck)
fmt.Scanln(&answer)
^
cmd/formatter/logs.go:118:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
^
cmd/formatter/logs.go:120:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
^
pkg/progress/json.go:67:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/json.go:87:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/plain.go:47:14: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
^
pkg/progress/tty.go:162:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, b.Column(0).ANSI)
^
pkg/progress/tty.go:165:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, aec.Hide)
^
pkg/compose/attach.go:53:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
^
pkg/compose/compose.go:194:6: emptyStringTest: replace `len(dependencies) > 0` with `dependencies != ""` (gocritic)
if len(dependencies) > 0 {
^
pkg/compose/convergence.go:461:2: builtinShadow: shadowing of predeclared identifier: max (gocritic)
max := 0
^
pkg/compose/run.go:127:5: emptyStringTest: replace `len(opts.User) > 0` with `opts.User != ""` (gocritic)
if len(opts.User) > 0 {
^
pkg/compose/run.go:139:5: emptyStringTest: replace `len(opts.WorkingDir) > 0` with `opts.WorkingDir != ""` (gocritic)
if len(opts.WorkingDir) > 0 {
^
pkg/compose/viz.go:91:8: emptyStringTest: replace `len(portConfig.HostIP) > 0` with `portConfig.HostIP != ""` (gocritic)
if len(portConfig.HostIP) > 0 {
^
cmd/compatibility/convert.go:66:6: emptyStringTest: replace `len(arg) > 0` with `arg != ""` (gocritic)
if len(arg) > 0 && arg[0] != '-' {
^
pkg/e2e/watch_test.go:208:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(res.Combined())
^
pkg/e2e/watch_test.go:290:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(r.Combined())
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-11 12:52:55 +02:00
|
|
|
_, _ = fmt.Fprintln(s.stderr(), "WARNING: --memory is not supported by BuildKit and will be ignored")
|
2023-05-08 15:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buildOptions, err := s.toBuildOptions(project, service, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-02-26 10:50:22 +01:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(attribute.String("builder", "buildkit"))
|
2023-11-27 11:02:19 +01:00
|
|
|
digest, err := s.doBuildBuildkit(ctx, name, buildOptions, w, nodes)
|
2023-05-08 15:21:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-27 10:14:31 +01:00
|
|
|
builtDigests[getServiceIndex(name)] = digest
|
2023-05-08 15:21:41 +02:00
|
|
|
|
2023-03-14 13:33:33 +01:00
|
|
|
return nil
|
2023-01-03 10:09:05 +01:00
|
|
|
}, func(traversal *graphTraversal) {
|
|
|
|
traversal.maxConcurrency = s.maxConcurrency
|
|
|
|
})
|
2023-03-14 13:33:33 +01:00
|
|
|
|
2023-05-08 15:21:41 +02:00
|
|
|
// enforce all build event get consumed
|
2023-07-24 16:11:06 +02:00
|
|
|
if buildkitEnabled {
|
|
|
|
if errw := w.Wait(); errw != nil {
|
|
|
|
return nil, errw
|
|
|
|
}
|
2023-05-08 15:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:36:48 -04:00
|
|
|
for i, imageDigest := range builtDigests {
|
|
|
|
if imageDigest != "" {
|
2024-02-09 11:40:29 +01:00
|
|
|
service := project.Services[names[i]]
|
|
|
|
imageRef := api.GetImageNameOrDefault(service, project.Name)
|
2023-07-17 10:36:48 -04:00
|
|
|
imageIDs[imageRef] = imageDigest
|
2025-01-08 15:55:05 +01:00
|
|
|
cw.Event(progress.BuiltEvent(names[i]))
|
2023-03-14 13:33:33 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-13 13:33:40 +01:00
|
|
|
return imageIDs, err
|
2021-02-24 17:21:10 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project, buildOpts *api.BuildOptions, quietPull bool) error {
|
2023-11-27 11:02:19 +01:00
|
|
|
for name, service := range project.Services {
|
2025-04-02 17:37:34 +02:00
|
|
|
if service.Provider == nil && service.Image == "" && service.Build == nil {
|
2023-11-27 11:02:19 +01:00
|
|
|
return fmt.Errorf("invalid service %q. Must specify either image or build", name)
|
2021-06-02 18:25:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
images, err := s.getLocalImagesDigests(ctx, project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-12 14:52:58 +00:00
|
|
|
err = tracing.SpanWrapFunc("project/pull", tracing.ProjectOptions(ctx, project),
|
2023-07-18 23:53:10 -04:00
|
|
|
func(ctx context.Context) error {
|
|
|
|
return s.pullRequiredImages(ctx, project, images, quietPull)
|
|
|
|
},
|
|
|
|
)(ctx)
|
2021-04-19 11:04:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
if buildOpts != nil {
|
2024-02-12 14:52:58 +00:00
|
|
|
err = tracing.SpanWrapFunc("project/build", tracing.ProjectOptions(ctx, project),
|
2023-07-18 23:53:10 -04:00
|
|
|
func(ctx context.Context) error {
|
2023-08-30 08:47:09 -04:00
|
|
|
builtImages, err := s.build(ctx, project, *buildOpts, images)
|
2023-07-18 23:53:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, digest := range builtImages {
|
2025-02-21 11:05:12 +01:00
|
|
|
images[name] = api.ImageSummary{
|
|
|
|
Repository: name,
|
|
|
|
ID: digest,
|
|
|
|
LastTagTime: time.Now(),
|
|
|
|
}
|
2023-07-18 23:53:10 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)(ctx)
|
2023-05-25 15:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-19 11:04:29 +02:00
|
|
|
}
|
2023-05-25 15:33:43 +02:00
|
|
|
|
2021-08-24 10:50:44 +02:00
|
|
|
// set digest as com.docker.compose.image label so we can detect outdated containers
|
2023-11-27 11:02:19 +01:00
|
|
|
for name, service := range project.Services {
|
2022-02-16 15:51:10 +01:00
|
|
|
image := api.GetImageNameOrDefault(service, project.Name)
|
2025-02-21 11:05:12 +01:00
|
|
|
img, ok := images[image]
|
2021-04-19 11:04:29 +02:00
|
|
|
if ok {
|
2025-02-21 11:05:12 +01:00
|
|
|
service.CustomLabels.Add(api.ImageDigestLabel, img.ID)
|
2021-04-19 11:04:29 +02:00
|
|
|
}
|
2023-11-27 11:02:19 +01:00
|
|
|
project.Services[name] = service
|
2021-04-19 11:04:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:05:12 +01:00
|
|
|
func (s *composeService) getLocalImagesDigests(ctx context.Context, project *types.Project) (map[string]api.ImageSummary, error) {
|
2025-04-09 09:52:31 +02:00
|
|
|
imageNames := utils.Set[string]{}
|
2021-04-13 11:31:39 +02:00
|
|
|
for _, s := range project.Services {
|
2025-04-08 19:44:26 +02:00
|
|
|
imageNames.Add(api.GetImageNameOrDefault(s, project.Name))
|
2025-04-09 09:52:31 +02:00
|
|
|
for _, volume := range s.Volumes {
|
|
|
|
if volume.Type == types.VolumeTypeImage {
|
|
|
|
imageNames.Add(volume.Source)
|
|
|
|
}
|
2021-03-29 17:45:03 +02:00
|
|
|
}
|
2021-02-24 17:21:10 +01:00
|
|
|
}
|
2025-04-09 09:52:31 +02:00
|
|
|
imgs, err := s.getImageSummaries(ctx, imageNames.Elements())
|
2021-04-13 11:31:39 +02:00
|
|
|
if err != nil {
|
2021-04-19 11:04:29 +02:00
|
|
|
return nil, err
|
2021-04-13 11:31:39 +02:00
|
|
|
}
|
2022-03-10 08:27:36 +01:00
|
|
|
|
2023-05-10 10:44:12 +02:00
|
|
|
for i, service := range project.Services {
|
|
|
|
imgName := api.GetImageNameOrDefault(service, project.Name)
|
2025-02-21 11:05:12 +01:00
|
|
|
img, ok := imgs[imgName]
|
2023-05-16 23:47:56 +02:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-10 10:44:12 +02:00
|
|
|
if service.Platform != "" {
|
|
|
|
platform, err := platforms.Parse(service.Platform)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-21 11:05:12 +01:00
|
|
|
inspect, err := s.apiClient().ImageInspect(ctx, img.ID)
|
2023-05-10 10:44:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
actual := specs.Platform{
|
|
|
|
Architecture: inspect.Architecture,
|
|
|
|
OS: inspect.Os,
|
|
|
|
Variant: inspect.Variant,
|
|
|
|
}
|
|
|
|
if !platforms.NewMatcher(platform).Match(actual) {
|
2025-06-04 15:01:08 +02:00
|
|
|
logrus.Debugf("local image %s doesn't match expected platform %s", service.Image, service.Platform)
|
2023-08-30 08:47:09 -04:00
|
|
|
// there is a local image, but it's for the wrong platform, so
|
|
|
|
// pretend it doesn't exist so that we can pull/build an image
|
|
|
|
// for the correct platform instead
|
2025-02-21 11:05:12 +01:00
|
|
|
delete(imgs, imgName)
|
2023-05-10 10:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:05:12 +01:00
|
|
|
project.Services[i].CustomLabels.Add(api.ImageDigestLabel, img.ID)
|
2023-05-16 23:47:56 +02:00
|
|
|
|
2022-03-10 08:27:36 +01:00
|
|
|
}
|
|
|
|
|
2025-02-21 11:05:12 +01:00
|
|
|
return imgs, nil
|
2020-11-25 16:51:54 +01:00
|
|
|
}
|
|
|
|
|
2023-08-10 08:57:28 -04:00
|
|
|
// resolveAndMergeBuildArgs returns the final set of build arguments to use for the service image build.
|
|
|
|
//
|
|
|
|
// First, args directly defined via `build.args` in YAML are considered.
|
|
|
|
// Then, any explicitly passed args in opts (e.g. via `--build-arg` on the CLI) are merged, overwriting any
|
|
|
|
// keys that already exist.
|
|
|
|
// Next, any keys without a value are resolved using the project environment.
|
|
|
|
//
|
|
|
|
// Finally, standard proxy variables based on the Docker client configuration are added, but will not overwrite
|
|
|
|
// any values if already present.
|
2024-02-09 11:40:29 +01:00
|
|
|
func resolveAndMergeBuildArgs(dockerCli command.Cli, project *types.Project, service types.ServiceConfig, opts api.BuildOptions) types.MappingWithEquals {
|
2023-08-10 08:57:28 -04:00
|
|
|
result := make(types.MappingWithEquals).
|
|
|
|
OverrideBy(service.Build.Args).
|
|
|
|
OverrideBy(opts.Args).
|
|
|
|
Resolve(envResolver(project.Environment))
|
|
|
|
|
|
|
|
// proxy arguments do NOT override and should NOT have env resolution applied,
|
|
|
|
// so they're handled last
|
|
|
|
for k, v := range storeutil.GetProxyConfig(dockerCli) {
|
|
|
|
if _, ok := result[k]; !ok {
|
2023-08-17 20:25:28 +02:00
|
|
|
v := v
|
2023-08-10 08:57:28 -04:00
|
|
|
result[k] = &v
|
2023-01-10 10:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-10 08:57:28 -04:00
|
|
|
return result
|
|
|
|
}
|
2023-01-10 10:18:09 +01:00
|
|
|
|
2023-08-10 08:57:28 -04:00
|
|
|
func (s *composeService) toBuildOptions(project *types.Project, service types.ServiceConfig, options api.BuildOptions) (build.Options, error) {
|
2023-08-30 08:47:09 -04:00
|
|
|
plats, err := parsePlatforms(service)
|
2022-08-08 16:03:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
2021-02-27 07:43:37 -03:00
|
|
|
}
|
|
|
|
|
2022-03-02 15:21:26 +01:00
|
|
|
cacheFrom, err := buildflags.ParseCacheEntry(service.Build.CacheFrom)
|
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
|
|
|
}
|
|
|
|
cacheTo, err := buildflags.ParseCacheEntry(service.Build.CacheTo)
|
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
|
|
|
}
|
|
|
|
|
2022-04-01 23:09:19 +02:00
|
|
|
sessionConfig := []session.Attachable{
|
2025-02-12 09:23:07 +01:00
|
|
|
authprovider.NewDockerAuthProvider(authprovider.DockerAuthProviderConfig{
|
|
|
|
ConfigFile: s.configFile(),
|
|
|
|
}),
|
2022-04-01 23:09:19 +02:00
|
|
|
}
|
2023-03-15 17:19:55 +01:00
|
|
|
if len(options.SSHs) > 0 || len(service.Build.SSH) > 0 {
|
|
|
|
sshAgentProvider, err := sshAgentProvider(append(service.Build.SSH, options.SSHs...))
|
2022-04-01 23:09:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
|
|
|
}
|
|
|
|
sessionConfig = append(sessionConfig, sshAgentProvider)
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:37:43 +02:00
|
|
|
if len(service.Build.Secrets) > 0 {
|
2022-07-22 17:37:42 +02:00
|
|
|
secretsProvider, err := addSecretsConfig(project, service)
|
2022-04-13 16:37:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
|
|
|
}
|
2022-07-01 10:41:48 +02:00
|
|
|
sessionConfig = append(sessionConfig, secretsProvider)
|
2022-04-13 16:37:43 +02:00
|
|
|
}
|
|
|
|
|
2023-07-17 10:36:48 -04:00
|
|
|
tags := []string{api.GetImageNameOrDefault(service, project.Name)}
|
2022-05-18 14:43:54 +02:00
|
|
|
if len(service.Build.Tags) > 0 {
|
|
|
|
tags = append(tags, service.Build.Tags...)
|
|
|
|
}
|
2024-04-10 12:27:49 +02:00
|
|
|
|
|
|
|
allow, err := buildflags.ParseEntitlements(service.Build.Entitlements)
|
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
|
|
|
}
|
2022-12-21 10:20:46 +01:00
|
|
|
if service.Build.Privileged {
|
2025-02-12 09:34:07 +01:00
|
|
|
allow = append(allow, entitlements.EntitlementSecurityInsecure.String())
|
2022-12-21 10:20:46 +01:00
|
|
|
}
|
2022-05-18 14:43:54 +02:00
|
|
|
|
build: label built images for reliable cleanup on `down`
When running `compose down`, the `--rmi` flag can be passed,
which currently supports two values:
* `local`: remove any _implicitly-named_ images that Compose
built
* `all` : remove any named images (locally-built or fetched
from a remote repo)
Removing images in the `local` case can be problematic, as it's
historically been done via a fair amount of inference over the
Compose model. Additionally, when using the "project-model"
(by passing `--project-name` instead of using a Compose file),
we're even more limited: if no containers for the project are
running, there's nothing to derive state from to perform the
inference on.
As a first pass, we started labeling _containers_ with the name
of the locally-built image associated with it (if any) in #9715.
Unfortunately, this still suffers from the aforementioned problems
around using actual state (i.e. the containers might no longer
exist) and meant that when operating in file mode (the default),
things did not behave as expected: the label is not available
in the project since it only exists at runtime.
Now, with these changes, Compose will label any images it builds
with project metadata. Upon cleanup during `down`, the engine
image API is queried for related images and matched up with the
services for the project. As a fallback for images built with
prior versions of Compose, the previous approach is still taken.
See also:
* https://github.com/docker/compose/issues/9655
* https://github.com/docker/compose/pull/9715
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2022-09-07 16:24:47 -04:00
|
|
|
imageLabels := getImageBuildLabels(project, service)
|
|
|
|
|
2023-07-17 10:36:48 -04:00
|
|
|
push := options.Push && service.Image != ""
|
2023-03-15 17:19:55 +01:00
|
|
|
exports := []bclient.ExportEntry{{
|
|
|
|
Type: "docker",
|
|
|
|
Attrs: map[string]string{
|
|
|
|
"load": "true",
|
2023-07-17 10:36:48 -04:00
|
|
|
"push": fmt.Sprint(push),
|
2023-03-15 17:19:55 +01:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
if len(service.Build.Platforms) > 1 {
|
|
|
|
exports = []bclient.ExportEntry{{
|
|
|
|
Type: "image",
|
|
|
|
Attrs: map[string]string{
|
2023-07-17 10:36:48 -04:00
|
|
|
"push": fmt.Sprint(push),
|
2023-03-15 17:19:55 +01:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-01-08 22:13:26 +00:00
|
|
|
sp, err := build.ReadSourcePolicy()
|
|
|
|
if err != nil {
|
|
|
|
return build.Options{}, err
|
|
|
|
}
|
|
|
|
|
2025-05-20 16:00:33 +02:00
|
|
|
attests := map[string]*string{}
|
2025-05-20 17:14:54 +02:00
|
|
|
if !options.Provenance {
|
|
|
|
attests["provenance"] = nil
|
|
|
|
}
|
2025-05-20 16:00:33 +02:00
|
|
|
|
2021-04-15 16:52:45 +02:00
|
|
|
return build.Options{
|
|
|
|
Inputs: build.Inputs{
|
2023-03-03 11:36:12 +01:00
|
|
|
ContextPath: service.Build.Context,
|
|
|
|
DockerfileInline: service.Build.DockerfileInline,
|
|
|
|
DockerfilePath: dockerFilePath(service.Build.Context, service.Build.Dockerfile),
|
2025-02-25 09:16:14 +01:00
|
|
|
NamedContexts: toBuildContexts(service, project),
|
2021-04-15 16:52:45 +02:00
|
|
|
},
|
2025-01-23 08:50:54 +01:00
|
|
|
CacheFrom: pb.CreateCaches(cacheFrom.ToPB()),
|
|
|
|
CacheTo: pb.CreateCaches(cacheTo.ToPB()),
|
2024-01-08 22:13:26 +00:00
|
|
|
NoCache: service.Build.NoCache,
|
|
|
|
Pull: service.Build.Pull,
|
|
|
|
BuildArgs: flatten(resolveAndMergeBuildArgs(s.dockerCli, project, service, options)),
|
|
|
|
Tags: tags,
|
|
|
|
Target: service.Build.Target,
|
|
|
|
Exports: exports,
|
|
|
|
Platforms: plats,
|
|
|
|
Labels: imageLabels,
|
|
|
|
NetworkMode: service.Build.Network,
|
|
|
|
ExtraHosts: service.Build.ExtraHosts.AsList(":"),
|
|
|
|
Ulimits: toUlimitOpt(service.Build.Ulimits),
|
|
|
|
Session: sessionConfig,
|
|
|
|
Allow: allow,
|
|
|
|
SourcePolicy: sp,
|
2025-05-20 16:00:33 +02:00
|
|
|
Attests: attests,
|
2021-02-27 07:43:37 -03:00
|
|
|
}, nil
|
2020-11-25 12:17:33 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 19:10:29 +01:00
|
|
|
func toUlimitOpt(ulimits map[string]*types.UlimitsConfig) *cliopts.UlimitOpt {
|
2024-06-07 17:57:54 +02:00
|
|
|
ref := map[string]*container.Ulimit{}
|
2023-11-13 19:10:29 +01:00
|
|
|
for _, limit := range toUlimits(ulimits) {
|
2024-06-07 17:57:54 +02:00
|
|
|
ref[limit.Name] = &container.Ulimit{
|
2023-11-13 19:10:29 +01:00
|
|
|
Name: limit.Name,
|
|
|
|
Hard: limit.Hard,
|
|
|
|
Soft: limit.Soft,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cliopts.NewUlimitOpt(&ref)
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:30:22 +02:00
|
|
|
func flatten(in types.MappingWithEquals) types.Mapping {
|
2023-02-07 20:50:15 +01:00
|
|
|
out := types.Mapping{}
|
2020-11-25 12:17:33 +01:00
|
|
|
if len(in) == 0 {
|
2023-02-07 20:50:15 +01:00
|
|
|
return out
|
2020-11-25 12:17:33 +01:00
|
|
|
}
|
|
|
|
for k, v := range in {
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out[k] = *v
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-03-30 11:47:34 +02:00
|
|
|
func sshAgentProvider(sshKeys types.SSHConfig) (session.Attachable, error) {
|
|
|
|
sshConfig := make([]sshprovider.AgentConfig, 0, len(sshKeys))
|
|
|
|
for _, sshKey := range sshKeys {
|
|
|
|
sshConfig = append(sshConfig, sshprovider.AgentConfig{
|
|
|
|
ID: sshKey.ID,
|
|
|
|
Paths: []string{sshKey.Path},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sshprovider.NewSSHAgentProvider(sshConfig)
|
|
|
|
}
|
2022-07-01 10:41:48 +02:00
|
|
|
|
2022-07-22 17:37:42 +02:00
|
|
|
func addSecretsConfig(project *types.Project, service types.ServiceConfig) (session.Attachable, error) {
|
2022-07-01 10:41:48 +02:00
|
|
|
var sources []secretsprovider.Source
|
|
|
|
for _, secret := range service.Build.Secrets {
|
|
|
|
config := project.Secrets[secret.Source]
|
2023-05-04 10:01:10 +02:00
|
|
|
id := secret.Source
|
|
|
|
if secret.Target != "" {
|
|
|
|
id = secret.Target
|
|
|
|
}
|
2022-07-01 10:41:48 +02:00
|
|
|
switch {
|
|
|
|
case config.File != "":
|
|
|
|
sources = append(sources, secretsprovider.Source{
|
2023-05-04 10:01:10 +02:00
|
|
|
ID: id,
|
2022-07-01 10:41:48 +02:00
|
|
|
FilePath: config.File,
|
|
|
|
})
|
|
|
|
case config.Environment != "":
|
|
|
|
sources = append(sources, secretsprovider.Source{
|
2023-05-04 10:01:10 +02:00
|
|
|
ID: id,
|
2022-07-01 10:41:48 +02:00
|
|
|
Env: config.Environment,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("build.secrets only supports environment or file-based secrets: %q", secret.Source)
|
|
|
|
}
|
2023-06-14 10:20:36 +02:00
|
|
|
if secret.UID != "" || secret.GID != "" || secret.Mode != nil {
|
|
|
|
logrus.Warn("secrets `uid`, `gid` and `mode` are not supported by BuildKit, they will be ignored")
|
|
|
|
}
|
2022-07-01 10:41:48 +02:00
|
|
|
}
|
|
|
|
store, err := secretsprovider.NewStore(sources)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return secretsprovider.NewSecretProvider(store), nil
|
|
|
|
}
|
build: label built images for reliable cleanup on `down`
When running `compose down`, the `--rmi` flag can be passed,
which currently supports two values:
* `local`: remove any _implicitly-named_ images that Compose
built
* `all` : remove any named images (locally-built or fetched
from a remote repo)
Removing images in the `local` case can be problematic, as it's
historically been done via a fair amount of inference over the
Compose model. Additionally, when using the "project-model"
(by passing `--project-name` instead of using a Compose file),
we're even more limited: if no containers for the project are
running, there's nothing to derive state from to perform the
inference on.
As a first pass, we started labeling _containers_ with the name
of the locally-built image associated with it (if any) in #9715.
Unfortunately, this still suffers from the aforementioned problems
around using actual state (i.e. the containers might no longer
exist) and meant that when operating in file mode (the default),
things did not behave as expected: the label is not available
in the project since it only exists at runtime.
Now, with these changes, Compose will label any images it builds
with project metadata. Upon cleanup during `down`, the engine
image API is queried for related images and matched up with the
services for the project. As a fallback for images built with
prior versions of Compose, the previous approach is still taken.
See also:
* https://github.com/docker/compose/issues/9655
* https://github.com/docker/compose/pull/9715
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2022-09-07 16:24:47 -04:00
|
|
|
|
|
|
|
func getImageBuildLabels(project *types.Project, service types.ServiceConfig) types.Labels {
|
|
|
|
ret := make(types.Labels)
|
|
|
|
if service.Build != nil {
|
|
|
|
for k, v := range service.Build.Labels {
|
|
|
|
ret.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Add(api.VersionLabel, api.ComposeVersion)
|
|
|
|
ret.Add(api.ProjectLabel, project.Name)
|
|
|
|
ret.Add(api.ServiceLabel, service.Name)
|
|
|
|
return ret
|
|
|
|
}
|
2022-09-13 18:00:41 +01:00
|
|
|
|
2025-02-25 09:16:14 +01:00
|
|
|
func toBuildContexts(service types.ServiceConfig, project *types.Project) map[string]build.NamedContext {
|
2023-02-16 22:58:09 +01:00
|
|
|
namedContexts := map[string]build.NamedContext{}
|
2025-02-25 09:16:14 +01:00
|
|
|
for name, contextPath := range service.Build.AdditionalContexts {
|
|
|
|
if strings.HasPrefix(contextPath, types.ServicePrefix) {
|
|
|
|
// image we depend on has been built previously, as we run in dependency order.
|
|
|
|
// so we convert the service reference into an image reference
|
|
|
|
target := contextPath[len(types.ServicePrefix):]
|
|
|
|
image := api.GetImageNameOrDefault(project.Services[target], project.Name)
|
|
|
|
contextPath = "docker-image://" + image
|
2025-01-22 14:41:52 +01:00
|
|
|
}
|
fix linting issues with golangci-lint 1.60.2
pkg/watch/watcher_darwin.go:96:16: Error return value of `d.stream.Start` is not checked (errcheck)
d.stream.Start()
^
pkg/prompt/prompt.go:97:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(u.stdout, message)
^
pkg/prompt/prompt.go:99:12: Error return value of `fmt.Scanln` is not checked (errcheck)
fmt.Scanln(&answer)
^
cmd/formatter/logs.go:118:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
^
cmd/formatter/logs.go:120:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
^
pkg/progress/json.go:67:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/json.go:87:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/plain.go:47:14: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
^
pkg/progress/tty.go:162:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, b.Column(0).ANSI)
^
pkg/progress/tty.go:165:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, aec.Hide)
^
pkg/compose/attach.go:53:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
^
pkg/compose/compose.go:194:6: emptyStringTest: replace `len(dependencies) > 0` with `dependencies != ""` (gocritic)
if len(dependencies) > 0 {
^
pkg/compose/convergence.go:461:2: builtinShadow: shadowing of predeclared identifier: max (gocritic)
max := 0
^
pkg/compose/run.go:127:5: emptyStringTest: replace `len(opts.User) > 0` with `opts.User != ""` (gocritic)
if len(opts.User) > 0 {
^
pkg/compose/run.go:139:5: emptyStringTest: replace `len(opts.WorkingDir) > 0` with `opts.WorkingDir != ""` (gocritic)
if len(opts.WorkingDir) > 0 {
^
pkg/compose/viz.go:91:8: emptyStringTest: replace `len(portConfig.HostIP) > 0` with `portConfig.HostIP != ""` (gocritic)
if len(portConfig.HostIP) > 0 {
^
cmd/compatibility/convert.go:66:6: emptyStringTest: replace `len(arg) > 0` with `arg != ""` (gocritic)
if len(arg) > 0 && arg[0] != '-' {
^
pkg/e2e/watch_test.go:208:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(res.Combined())
^
pkg/e2e/watch_test.go:290:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(r.Combined())
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-11 12:52:55 +02:00
|
|
|
namedContexts[name] = build.NamedContext{Path: contextPath}
|
2023-02-16 22:58:09 +01:00
|
|
|
}
|
|
|
|
return namedContexts
|
|
|
|
}
|
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
func parsePlatforms(service types.ServiceConfig) ([]specs.Platform, error) {
|
|
|
|
if service.Build == nil || len(service.Build.Platforms) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
ret := make([]specs.Platform, len(service.Build.Platforms))
|
|
|
|
for i := range service.Build.Platforms {
|
|
|
|
p, err := platforms.Parse(service.Build.Platforms[i])
|
2022-08-31 20:53:41 +02:00
|
|
|
if err != nil {
|
2023-08-30 08:47:09 -04:00
|
|
|
errs = append(errs, err)
|
|
|
|
} else {
|
|
|
|
ret[i] = p
|
2022-08-31 20:53:41 +02:00
|
|
|
}
|
2022-08-08 16:03:36 +02:00
|
|
|
}
|
2022-09-22 12:06:34 +02:00
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
if err := errors.Join(errs...); err != nil {
|
|
|
|
return nil, err
|
2022-09-22 12:06:34 +02:00
|
|
|
}
|
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
return ret, nil
|
2022-09-22 12:06:34 +02:00
|
|
|
}
|
2025-04-28 08:59:07 +02:00
|
|
|
|
|
|
|
func addBuildDependencies(services []string, project *types.Project) []string {
|
|
|
|
servicesWithDependencies := utils.NewSet(services...)
|
|
|
|
for _, service := range services {
|
2025-06-12 08:39:41 +02:00
|
|
|
s, ok := project.Services[service]
|
|
|
|
if !ok {
|
|
|
|
s = project.DisabledServices[service]
|
|
|
|
}
|
|
|
|
b := s.Build
|
2025-04-28 08:59:07 +02:00
|
|
|
if b != nil {
|
|
|
|
for _, target := range b.AdditionalContexts {
|
|
|
|
if s, found := strings.CutPrefix(target, types.ServicePrefix); found {
|
|
|
|
servicesWithDependencies.Add(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(servicesWithDependencies) > len(services) {
|
|
|
|
return addBuildDependencies(servicesWithDependencies.Elements(), project)
|
|
|
|
}
|
|
|
|
return servicesWithDependencies.Elements()
|
|
|
|
}
|