2020-11-30 12:03:13 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-30 15:24:36 +02:00
|
|
|
"fmt"
|
2021-02-16 12:10:35 +01:00
|
|
|
"os"
|
2021-11-17 08:21:55 +01:00
|
|
|
"strings"
|
2020-11-30 12:03:13 +01:00
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/cli"
|
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2023-08-30 10:15:35 +02:00
|
|
|
"github.com/docker/cli/cli/command"
|
2023-04-24 10:51:40 +02:00
|
|
|
cliopts "github.com/docker/cli/opts"
|
2023-06-12 09:27:32 +02:00
|
|
|
ui "github.com/docker/compose/v2/pkg/progress"
|
2020-11-30 12:03:13 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2020-11-30 12:03:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type buildOptions struct {
|
2022-12-19 16:38:36 +00:00
|
|
|
*ProjectOptions
|
2025-05-20 16:00:33 +02:00
|
|
|
quiet bool
|
|
|
|
pull bool
|
|
|
|
push bool
|
|
|
|
args []string
|
|
|
|
noCache bool
|
|
|
|
memory cliopts.MemBytes
|
|
|
|
ssh string
|
|
|
|
builder string
|
|
|
|
deps bool
|
|
|
|
print bool
|
|
|
|
check bool
|
2025-05-20 17:14:54 +02:00
|
|
|
provenance bool
|
2022-03-30 11:47:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
|
|
|
|
var SSHKeys []types.SSHKey
|
|
|
|
if opts.ssh != "" {
|
2023-11-08 10:19:24 +01:00
|
|
|
id, path, found := strings.Cut(opts.ssh, "=")
|
|
|
|
if !found && id != "default" {
|
|
|
|
return api.BuildOptions{}, fmt.Errorf("invalid ssh key %q", opts.ssh)
|
|
|
|
}
|
|
|
|
SSHKeys = append(SSHKeys, types.SSHKey{
|
|
|
|
ID: id,
|
|
|
|
Path: path,
|
|
|
|
})
|
2022-03-30 11:47:34 +02:00
|
|
|
}
|
2023-06-27 12:32:47 +02:00
|
|
|
builderName := opts.builder
|
|
|
|
if builderName == "" {
|
|
|
|
builderName = os.Getenv("BUILDX_BUILDER")
|
|
|
|
}
|
2022-03-30 11:47:34 +02:00
|
|
|
|
2024-02-08 16:31:29 +01:00
|
|
|
uiMode := ui.Mode
|
|
|
|
if uiMode == ui.ModeJSON {
|
2024-03-16 17:10:58 +01:00
|
|
|
uiMode = "rawjson"
|
2024-02-08 16:31:29 +01:00
|
|
|
}
|
2025-05-20 16:00:33 +02:00
|
|
|
|
2022-03-30 11:47:34 +02:00
|
|
|
return api.BuildOptions{
|
2025-05-20 16:00:33 +02:00
|
|
|
Pull: opts.pull,
|
|
|
|
Push: opts.push,
|
|
|
|
Progress: uiMode,
|
|
|
|
Args: types.NewMappingWithEquals(opts.args),
|
|
|
|
NoCache: opts.noCache,
|
|
|
|
Quiet: opts.quiet,
|
|
|
|
Services: services,
|
|
|
|
Deps: opts.deps,
|
|
|
|
Memory: int64(opts.memory),
|
|
|
|
Print: opts.print,
|
|
|
|
Check: opts.check,
|
|
|
|
SSHs: SSHKeys,
|
|
|
|
Builder: builderName,
|
2025-05-20 17:14:54 +02:00
|
|
|
Provenance: opts.provenance,
|
2022-03-30 11:47:34 +02:00
|
|
|
}, nil
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func buildCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
|
2021-01-20 11:02:46 +01:00
|
|
|
opts := buildOptions{
|
2022-12-19 16:38:36 +00:00
|
|
|
ProjectOptions: p,
|
2021-01-20 11:02:46 +01:00
|
|
|
}
|
2021-02-16 12:10:35 +01:00
|
|
|
cmd := &cobra.Command{
|
2022-08-02 13:11:57 +02:00
|
|
|
Use: "build [OPTIONS] [SERVICE...]",
|
2020-12-10 15:43:20 +01:00
|
|
|
Short: "Build or rebuild services",
|
2021-05-19 18:49:33 +02:00
|
|
|
PreRunE: Adapt(func(ctx context.Context, args []string) error {
|
2021-02-16 12:10:35 +01:00
|
|
|
if opts.quiet {
|
2023-06-12 09:27:32 +02:00
|
|
|
ui.Mode = ui.ModeQuiet
|
2021-02-16 12:10:35 +01:00
|
|
|
devnull, err := os.Open(os.DevNull)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.Stdout = devnull
|
|
|
|
}
|
2021-05-19 18:49:33 +02:00
|
|
|
return nil
|
|
|
|
}),
|
2022-03-30 11:47:34 +02:00
|
|
|
RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
|
|
|
|
if cmd.Flags().Changed("ssh") && opts.ssh == "" {
|
|
|
|
opts.ssh = "default"
|
|
|
|
}
|
2023-06-12 09:27:32 +02:00
|
|
|
if cmd.Flags().Changed("progress") && opts.ssh == "" {
|
2023-11-08 16:14:30 +01:00
|
|
|
fmt.Fprint(os.Stderr, "--progress is a global compose flag, better use `docker compose --progress xx build ...\n")
|
2022-11-24 15:42:28 +01:00
|
|
|
}
|
2023-08-30 10:15:35 +02:00
|
|
|
return runBuild(ctx, dockerCli, backend, opts, args)
|
2021-04-15 12:43:18 +02:00
|
|
|
}),
|
2023-08-30 10:15:35 +02:00
|
|
|
ValidArgsFunction: completeServiceNames(dockerCli, p),
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
2023-12-20 15:18:29 +01:00
|
|
|
flags := cmd.Flags()
|
2024-02-14 20:02:37 +01:00
|
|
|
flags.BoolVar(&opts.push, "push", false, "Push service images")
|
2023-12-20 15:18:29 +01:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
|
2024-02-14 20:02:37 +01:00
|
|
|
flags.BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image")
|
|
|
|
flags.StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services")
|
2023-12-20 15:18:29 +01:00
|
|
|
flags.StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
|
2024-02-14 20:02:37 +01:00
|
|
|
flags.StringVar(&opts.builder, "builder", "", "Set builder to use")
|
|
|
|
flags.BoolVar(&opts.deps, "with-dependencies", false, "Also build dependencies (transitively)")
|
2023-12-20 15:18:29 +01:00
|
|
|
|
|
|
|
flags.Bool("parallel", true, "Build images in parallel. DEPRECATED")
|
|
|
|
flags.MarkHidden("parallel") //nolint:errcheck
|
|
|
|
flags.Bool("compress", true, "Compress the build context using gzip. DEPRECATED")
|
|
|
|
flags.MarkHidden("compress") //nolint:errcheck
|
|
|
|
flags.Bool("force-rm", true, "Always remove intermediate containers. DEPRECATED")
|
|
|
|
flags.MarkHidden("force-rm") //nolint:errcheck
|
|
|
|
flags.BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
|
|
|
|
flags.Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
|
|
|
|
flags.MarkHidden("no-rm") //nolint:errcheck
|
|
|
|
flags.VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
|
2025-06-02 10:40:41 +02:00
|
|
|
flags.StringVar(&p.Progress, "progress", "", fmt.Sprintf(`Set type of ui output (%s)`, strings.Join(printerModes, ", ")))
|
2023-12-20 15:18:29 +01:00
|
|
|
flags.MarkHidden("progress") //nolint:errcheck
|
2025-03-26 11:23:42 +01:00
|
|
|
flags.BoolVar(&opts.print, "print", false, "Print equivalent bake file")
|
2025-04-22 16:42:18 +02:00
|
|
|
flags.BoolVar(&opts.check, "check", false, "Check build configuration")
|
2021-03-19 17:37:08 +01:00
|
|
|
|
2021-02-16 12:10:35 +01:00
|
|
|
return cmd
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:15:35 +02:00
|
|
|
func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, opts buildOptions, services []string) error {
|
2025-04-28 08:59:07 +02:00
|
|
|
project, _, err := opts.ToProject(ctx, dockerCli, nil, cli.WithResolvedPaths(true), cli.WithoutEnvironmentResolution)
|
2021-02-02 11:42:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-30 12:03:13 +01:00
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
if err := applyPlatforms(project, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-30 11:47:34 +02:00
|
|
|
apiBuildOptions, err := opts.toAPIBuildOptions(services)
|
2025-05-20 17:14:54 +02:00
|
|
|
apiBuildOptions.Provenance = true
|
2022-03-30 11:47:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-24 10:51:40 +02:00
|
|
|
|
2022-03-30 11:47:34 +02:00
|
|
|
return backend.Build(ctx, project, apiBuildOptions)
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|