Repository navigation
fix(scale): reject negative replica counts from CLI flags - #14588
AnAverageBeing wants to merge 1 commit into
Conversation
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>
|
This looks like a duplicate of #14483, which fixes the same issue (#14482). A maintainer has already approved it, it has been rebased, and it is waiting to be merged. If you think |
|
You are right, this is a duplicate of #14483 — thanks for catching it. I had missed it. I picked #14482 up from the The one substantive difference is that #14483 validates in Closing. Good luck with the merge. |
|
Closing as a duplicate of #14483, per the review above. |
What
scaleflags accept a negative replica count and exit 0. The compose file loaderalready rejects the same value.
All three previously succeeded.
web=abcwas already rejected,web=0is valid.Why it matters beyond input hygiene
pkg/compose/reconcile.gotestsexpectedScale == 0, not<= 0. With-1theremoval loop clears every observed container,
keptRunningstays false, and the== 0early return is skipped, so it goes on to plan pre-start hook runners for aservice 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.Validateand
UpOptions.Validatealready exist and both run before theirRun()body;composeService.Scalewas the one operation with no validate step, soapi.Compose.Scalewas unguarded entirely.parseServicesReplicasArgs,applyScaleOpts) check first too,so the error arrives before project load, and before the remote-stack
confirmation prompt that
upcan show.api.InvalidOptionError, matching the existing convention for anoption set to a value it doesn't accept.
Verification
Against a live engine (29.6.1), patched binary:
scale web=-1invalid option (scale): web=-1: replica count must be greater than or equal to 0up --scale web=-1 -dcreate --scale web=-1scale web=0scale web=2scale: -1Tests:
go test ./cmd/... ./pkg/api/... ./pkg/compose/...— passgo test -tags e2e ./internal/e2e/ -run 'TestScaleBasicCases|TestScaleWithDepsCases'— passgo vet ./cmd/... ./pkg/...— cleangofmt -l— cleanNew tests cover the parser (which had no unit test before this), the
ScaleOptions.Validatepath directly, andcomposeService.Scaleasserting theproject 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=abcused to print the rawstrconv.Atoierror from the linedirectly 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.
-1sentinel for replicas exists anywhere in the tree (--paralleland bake--jobsuse-1for unlimited, and are unrelated), so nothing regresses.runandwatchhave no--scaleflag;alphahas noscalesubcommand.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.mdis included. Scope note on the "link to an approvedissue" rule: #14482 already carries
status/approvedand I opened this against itrather 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