Repository navigation
fix(scale): reject negative replica counts from CLI flags - #14588
Open
AnAverageBeing wants to merge 1 commit into
Open
AnAverageBeing wants to merge 1 commit into
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 branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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