Skip to content

fix(scale): reject negative replica counts from CLI flags - #14588

Open
AnAverageBeing wants to merge 1 commit into
docker:mainfrom
AnAverageBeing:fix/reject-negative-scale-replicas
Open

AnAverageBeing wants to merge 1 commit into
docker:mainfrom
AnAverageBeing:fix/reject-negative-scale-replicas

Conversation

@AnAverageBeing

@AnAverageBeing AnAverageBeing commented Dec 5, 2026 •

Copy link
Copy Markdown

What

scale flags accept a negative replica count and exit 0. The compose file loader
already rejects the same value.

$ docker compose scale web=-1
$ docker compose up --scale web=-1 -d
$ docker compose create --scale web=-1

All three previously succeeded. web=abc was already rejected, web=0 is valid.

Why it matters beyond input hygiene

pkg/compose/reconcile.go tests expectedScale == 0, not <= 0. With -1 the
removal loop clears every observed container, keptRunning stays false, and the
== 0 early return is skipped, so it goes on to plan pre-start hook runners for a
service that will have zero replicas. The value is a distinct broken state, not
equivalent to 0 — which is why the backend has to refuse it, not just the flag
parser.

Change

  • api.ScaleOptions.Validate() rejects a negative count. CreateOptions.Validate
    and UpOptions.Validate already exist and both run before their Run() body;
    composeService.Scale was the one operation with no validate step, so
    api.Compose.Scale was unguarded entirely.
  • The CLI parsers (parseServicesReplicasArgs, applyScaleOpts) check first too,
    so the error arrives before project load, and before the remote-stack
    confirmation prompt that up can show.
  • Both raise api.InvalidOptionError, matching the existing convention for an
    option set to a value it doesn't accept.

Verification

Against a live engine (29.6.1), patched binary:

command before after
scale web=-1 exit 0, pulled image, created containers exit 1, invalid option (scale): web=-1: replica count must be greater than or equal to 0
up --scale web=-1 -d exit 0 exit 1
create --scale web=-1 exit 0, and removed the running container exit 1
scale web=0 exit 0 exit 0
scale web=2 exit 0 exit 0
file scale: -1 exit 1 exit 1, unchanged

Tests:

  • go test ./cmd/... ./pkg/api/... ./pkg/compose/... — pass
  • go test -tags e2e ./internal/e2e/ -run 'TestScaleBasicCases|TestScaleWithDepsCases' — pass
  • go vet ./cmd/... ./pkg/... — clean
  • gofmt -l — clean

New tests cover the parser (which had no unit test before this), the
ScaleOptions.Validate path directly, and composeService.Scale asserting the
project model is untouched when validation fails. With the validation removed the
negative cases fail, so they catch the bug rather than restating the code.

Scope notes

  • create --scale web=abc used to print the raw strconv.Atoi error from the line
    directly above the new check; it now gets invalid --scale option "web=abc": ....
    Adjacent and one line, but say the word if you'd rather keep that separate.
  • No -1 sentinel for replicas exists anywhere in the tree (--parallel and bake
    --jobs use -1 for unlimited, and are unrelated), so nothing regresses.
  • run and watch have no --scale flag; alpha has no scale subcommand.

AI assistance

Disclosed per the AI policy: drafted with Claude (Anthropic), running as an agent
in a terminal, under my direction. Tool, and roughly how much of the work it did:
the patch, the tests and this description were AI-drafted; I reviewed the diff,
ran the verification above myself against a real engine, and I'm accountable for
the change.

No AI_AGENT_DISCLOSURE.md is included. Scope note on the "link to an approved
issue" rule: #14482 already carries status/approved and I opened this against it
rather than filing a new issue; the commit sha proves the branch is contemporaneous
with the approval, since I couldn't have seen the label before it was applied.

Fixes #14482

The compose file loader already refuses a negative `scale:` or
`deploy.replicas:` with "must be greater than or equal to 0", but the
CLI accepted the same value through a flag:

    $ docker compose scale web=-1
    $ docker compose up --scale web=-1 -d
    $ docker compose create --scale web=-1

Those all exited 0. A negative count is not merely redundant with zero:
reconcile.go treats only `expectedScale == 0` as the scale-to-nothing
case, so -1 falls through to planning pre-start hook runners for a
service that will have no replicas.

Validate both entry points:

- ScaleOptions.Validate() so api.Compose.Scale refuses it too, matching
  CreateOptions.Validate and UpOptions.Validate, which both run before
  their Run() body.
- the CLI parsers, so the user gets the error before any project load or
  remote-stack prompt.

Both raise api.InvalidOptionError, the existing type for this case.

Also give `create --scale web=abc` a proper message instead of the raw
strconv error from the branch directly above.

Fixes docker#14482

Signed-off-by: AnAverageBeing <nottherealmcupvp17@gmail.com>
@AnAverageBeing
AnAverageBeing requested review from a team as code owners December 5, 2026 13:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Negative replica count in scale flags (docker compose scale / --scale) is accepted without validation

1 participant