Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions cmd/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type createOptions struct {
timeout int
quietPull bool
scale []string
labels []string // 👈 ADICIONADO
AssumeYes bool
}

Expand Down Expand Up @@ -84,6 +85,7 @@ func createCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Bac
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file")
flags.StringArrayVar(&opts.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
flags.StringArrayVar(&opts.labels, "label", []string{}, "Add or override labels on services") // 👈 ADICIONADO
flags.BoolVarP(&opts.AssumeYes, "yes", "y", false, `Assume "yes" as answer to all prompts and run non-interactively`)
flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
// assumeYes was introduced by mistake as `--y`
Expand Down Expand Up @@ -172,9 +174,7 @@ func (opts createOptions) Apply(project *types.Project) error {
project.Services[i] = service
}
}
// N.B. opts.Build means "force build all", but images can still be built
// when this is false
// e.g. if a service has pull_policy: build or its local image is policy

if opts.Build {
for i, service := range project.Services {
if service.Build == nil {
Expand All @@ -185,15 +185,33 @@ func (opts createOptions) Apply(project *types.Project) error {
}
}

if err := applyPlatforms(project, true); err != nil {
return err
// 👇 APLICA LABELS
if len(opts.labels) > 0 {
parsed := types.Labels{}
for _, l := range opts.labels {
parts := strings.SplitN(l, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid label %q, expected key=value", l)
}
parsed[parts[0]] = parts[1]
}

for name, svc := range project.Services {
if svc.Labels == nil {
svc.Labels = types.Labels{}
}
for k, v := range parsed {
svc.Labels[k] = v
}
project.Services[name] = svc
}
}

err := applyScaleOpts(project, opts.scale)
if err != nil {
if err := applyPlatforms(project, true); err != nil {
return err
}
return nil

return applyScaleOpts(project, opts.scale)
}

func applyScaleOpts(project *types.Project, opts []string) error {
Expand All @@ -217,8 +235,11 @@ func applyScaleOpts(project *types.Project, opts []string) error {

func (opts createOptions) isPullPolicyValid() bool {
pullPolicies := []string{
types.PullPolicyAlways, types.PullPolicyNever, types.PullPolicyBuild,
types.PullPolicyMissing, types.PullPolicyIfNotPresent,
types.PullPolicyAlways,
types.PullPolicyNever,
types.PullPolicyBuild,
types.PullPolicyMissing,
types.PullPolicyIfNotPresent,
}
return slices.Contains(pullPolicies, opts.Pull)
}
1 change: 1 addition & 0 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backend
flags.StringVar(&create.Pull, "pull", "policy", `Pull image before running ("always"|"missing"|"never")`)
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file")
flags.StringArrayVar(&create.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
flags.StringArrayVar(&create.labels, "label", []string{}, "Add or override labels on services, networks and volumes")
flags.BoolVar(&up.noColor, "no-color", false, "Produce monochrome output")
flags.BoolVar(&up.noPrefix, "no-log-prefix", false, "Don't print prefix in logs")
flags.BoolVar(&create.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed")
Expand Down