Skip to content

fix(cli): load the project from COMPOSE_FILE when -p is set - #14540

Open
glours wants to merge 2 commits into
docker:mainfrom
glours:fix-14535-compose-file-with-project-name
Open

glours wants to merge 2 commits into
docker:mainfrom
glours:fix-14535-compose-file-with-project-name

Conversation

@glours

@glours glours commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #14535

  • COMPOSE_FILE is now handled like -f when -p is set: the model is loaded, so stop runs pre_stop hooks instead of rebuilding a label-only project that doesn't carry them.
  • When the model fails to load, an explicit -p now takes precedence over COMPOSE_PROJECT_NAME in the label-driven fallback (previously the env var won, and commands targeted the wrong project).

projectOrName only loaded the Compose model when -f was passed or when
no project name was given. With -p and COMPOSE_FILE, the model was
skipped, so commands like stop received no project and rebuilt it from
container labels, which don't carry pre_stop hooks: the hooks were
silently not run.

COMPOSE_FILE is the environment equivalent of -f, so treat it the same
way when deciding to load the model.

Fixes docker#14535

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
@glours
glours requested review from a team as code owners November 20, 2026 14:45
@glours
glours requested a review from ndeloof November 20, 2026 14:45
@glours glours changed the title Fix 14535 compose file with project name fix(cli): load the project from COMPOSE_FILE when -p is set Nov 20, 2026

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

…ilure

When the Compose model can't be loaded, projectOrName falls back to the
label-driven project and used COMPOSE_PROJECT_NAME while ignoring an
explicit -p. With -p x and COMPOSE_PROJECT_NAME=y, commands such as ps,
down or rm then targeted project y instead of x.

Use -p first and COMPOSE_PROJECT_NAME only when -p is empty, matching
the usual precedence.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
@glours
glours force-pushed the fix-14535-compose-file-with-project-name branch from 7d54ab6 to e5a989e Compare November 20, 2026 15:03
@glours
glours requested a review from docker-agent November 20, 2026 15:03

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟡 NEEDS ATTENTION

Comment thread cmd/compose/compose.go Resolved
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ndeloof ndeloof left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not aligned with #14151 yet; the points below need a decision before merge.

Comment thread cmd/compose/compose.go
var project *types.Project
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
// COMPOSE_FILE is the environment equivalent of -f
if len(o.ConfigPaths) > 0 || os.Getenv(consts.ComposeFilePath) != "" || o.ProjectName == "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COMPOSE_FILE is treated as -f for loading, but not for the failure policy of #14151 (hard error for an explicit file, warning only for an implicit one). With both PRs, -p x + broken COMPOSE_FILE + COMPOSE_PROJECT_NAME falls back to label mode with a warning, which is what docker-agent's medium comment describes. Suggest a shared explicitFile() helper (ConfigPaths or COMPOSE_FILE) used here and by the hard-error check in #14151.

Also, os.Getenv misses a COMPOSE_FILE set in .env, which compose-go reads (WithConfigFileEnv uses the project environment). With -p, #14535 stays unfixed in that case: handle it or document the limit.

Behavior change worth stating in the description: -p x stop now fails on a stale/invalid COMPOSE_FILE or a missing required variable, where it used to work label-only.

Comment thread cmd/compose/compose.go
if envProjectName != "" {
return nil, envProjectName, nil
if envProjectName := os.Getenv(ComposeProjectName); envProjectName != "" {
// -p takes precedence over COMPOSE_PROJECT_NAME

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both PRs rewrite this block. After #14151 this fallback can't be reached with -p set (no load, or hard error), so the name-precedence change becomes dead code, and the -p wins… / …used without -p subtests (explicit ConfigPaths + missing file) turn into hard errors. Land #14151 first and rebase, or fold the two.

nit: add a COMPOSE_FILE-based variant to the fallback tests.

ndeloof added a commit to ndeloof/compose that referenced this pull request Nov 23, 2026
…wed load error, strict service validation

projectOrName and toProjectName resolved the project with opposite
precedences, and projectOrName silently swallowed any load error when
COMPOSE_PROJECT_NAME was set: a broken compose file sent stop, down,
ps... into label-based reconstruction without a word — even when the
file was named explicitly with --file.

One precedence now, documented on both resolvers and applied
identically by compose-go while loading: --project-name, then
COMPOSE_PROJECT_NAME, then the model's name. The failure policy
becomes explicit: an unreadable explicit --file is a hard error; no
file around with COMPOSE_PROJECT_NAME set stays the silent file-less
workflow; a present-but-broken implicit file falls back to label-based
mode with a warning.

Service-name validation follows one rule — strict whenever a model is
available: restart and wait no longer silently no-op on a typo
(validateServiceNames, profile-disabled services remain legitimate
targets), and the hand-rolled checks in ps and volumes are removed as
dead code, the load-time selection already rejecting unknown names
(pinned by test).

Epic docker#14074, F.4.

Rebased onto main, which since merged the jobs work (docker#14093, docker#14234):
projectOrName's job-target detection (jobTargetErr) is restored ahead
of the new explicit-file hard-error branch -- the file loaded fine
here, only the target's selection failed -- and validateServiceNames
now checks project.AllJobs() too, since restart/wait route their
service arguments through it instead of projectOrName's own selection.

docker-agent review: the "compose file found but could not be loaded"
warning's suppression guard only matched errdefs.IsNotFoundError
(compose-go's own ErrNotFound sentinel) -- a raw os.ErrNotExist (e.g. a
nonexistent --project-directory) wasn't recognized and would have
printed a misleading warning. Added errors.Is(err, os.ErrNotExist) as
a fallback, and TestProjectOrNameResolution now asserts the warning's
presence/absence in both directions instead of just the fallback name.

COMPOSE_FILE counts as an explicit file, like --file (explicitFile():
process environment or .env, as compose-go reads it): it is loaded even
with --project-name (docker#14535, also addressed by docker#14540), and
failing to load it is a hard error rather than a label-based fallback.
Consistent with docker#14540's reading of COMPOSE_FILE as the environment
equivalent of -f.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
ndeloof added a commit to ndeloof/compose that referenced this pull request Nov 23, 2026
…wed load error, strict service validation

projectOrName and toProjectName resolved the project with opposite
precedences, and projectOrName silently swallowed any load error when
COMPOSE_PROJECT_NAME was set: a broken compose file sent stop, down,
ps... into label-based reconstruction without a word — even when the
file was named explicitly with --file.

One precedence now, documented on both resolvers and applied
identically by compose-go while loading: --project-name, then
COMPOSE_PROJECT_NAME, then the model's name. The failure policy
becomes explicit: an unreadable explicit --file is a hard error; no
file around with COMPOSE_PROJECT_NAME set stays the silent file-less
workflow; a present-but-broken implicit file falls back to label-based
mode with a warning.

Service-name validation follows one rule — strict whenever a model is
available: restart and wait no longer silently no-op on a typo
(validateServiceNames, profile-disabled services remain legitimate
targets), and the hand-rolled checks in ps and volumes are removed as
dead code, the load-time selection already rejecting unknown names
(pinned by test).

Epic docker#14074, F.4.

Rebased onto main, which since merged the jobs work (docker#14093, docker#14234):
projectOrName's job-target detection (jobTargetErr) is restored ahead
of the new explicit-file hard-error branch -- the file loaded fine
here, only the target's selection failed -- and validateServiceNames
now checks project.AllJobs() too, since restart/wait route their
service arguments through it instead of projectOrName's own selection.

docker-agent review: the "compose file found but could not be loaded"
warning's suppression guard only matched errdefs.IsNotFoundError
(compose-go's own ErrNotFound sentinel) -- a raw os.ErrNotExist (e.g. a
nonexistent --project-directory) wasn't recognized and would have
printed a misleading warning. Added errors.Is(err, os.ErrNotExist) as
a fallback, and TestProjectOrNameResolution now asserts the warning's
presence/absence in both directions instead of just the fallback name.

COMPOSE_FILE counts as an explicit file, like --file (explicitFile():
process environment or .env, as compose-go reads it): it is loaded even
with --project-name (docker#14535, also addressed by docker#14540), and
failing to load it is a hard error rather than a label-based fallback.
Consistent with docker#14540's reading of COMPOSE_FILE as the environment
equivalent of -f.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
ndeloof added a commit to ndeloof/compose that referenced this pull request Nov 23, 2026
…wed load error, strict service validation

projectOrName and toProjectName resolved the project with opposite
precedences, and projectOrName silently swallowed any load error when
COMPOSE_PROJECT_NAME was set: a broken compose file sent stop, down,
ps... into label-based reconstruction without a word — even when the
file was named explicitly with --file.

One precedence now, documented on both resolvers and applied
identically by compose-go while loading: --project-name, then
COMPOSE_PROJECT_NAME, then the model's name. The failure policy
becomes explicit: an unreadable explicit --file is a hard error; no
file around with COMPOSE_PROJECT_NAME set stays the silent file-less
workflow; a present-but-broken implicit file falls back to label-based
mode with a warning.

Service-name validation follows one rule — strict whenever a model is
available: restart and wait no longer silently no-op on a typo
(validateServiceNames, profile-disabled services remain legitimate
targets), and the hand-rolled checks in ps and volumes are removed as
dead code, the load-time selection already rejecting unknown names
(pinned by test).

Epic docker#14074, F.4.

Rebased onto main, which since merged the jobs work (docker#14093, docker#14234):
projectOrName's job-target detection (jobTargetErr) is restored ahead
of the new explicit-file hard-error branch -- the file loaded fine
here, only the target's selection failed -- and validateServiceNames
now checks project.AllJobs() too, since restart/wait route their
service arguments through it instead of projectOrName's own selection.

docker-agent review: the "compose file found but could not be loaded"
warning's suppression guard only matched errdefs.IsNotFoundError
(compose-go's own ErrNotFound sentinel) -- a raw os.ErrNotExist (e.g. a
nonexistent --project-directory) wasn't recognized and would have
printed a misleading warning. Added errors.Is(err, os.ErrNotExist) as
a fallback, and TestProjectOrNameResolution now asserts the warning's
presence/absence in both directions instead of just the fallback name.

COMPOSE_FILE counts as an explicit file, like --file (explicitFile():
process environment or .env, as compose-go reads it): it is loaded even
with --project-name (docker#14535, also addressed by docker#14540), and
failing to load it is a hard error rather than a label-based fallback.
Consistent with docker#14540's reading of COMPOSE_FILE as the environment
equivalent of -f.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>

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] docker compose stop ignores COMPOSE_FILE when -p is used, skipping pre_stop hooks

3 participants