Skip to content

fix(watch): exclude Dockerfile and compose files from initial sync - #14117

Merged
glours merged 2 commits into
docker:mainfrom
htoyoda18:fix/watch-initial-sync-exclude-compose-files
Aug 24, 2026
Merged

fix(watch): exclude Dockerfile and compose files from initial sync#14117
glours merged 2 commits into
docker:mainfrom
htoyoda18:fix/watch-initial-sync-exclude-compose-files

Conversation

@htoyoda18

Copy link
Copy Markdown
Contributor

What I did

initialSync's doc comment promises that the Dockerfile and compose files are never copied into the container during docker compose watch's initial sync — but the matcher enforcing that was dropped without replacement in a January 2025 refactor (#12469), leaving only a // FIXME .dockerignore comment where it used to sit. Neither .dockerignore-derived ignores nor EphemeralPathMatcher() (editor swap/temp files) cover this, so a develop.watch trigger whose path includes the project root would copy Dockerfile/compose.yaml into the running container on initial sync.

Reintroduced a dedicated dockerFileIgnore matcher (Dockerfile, *compose*.y*ml) inside initialSync, matching the pre-refactor behavior.

Scoped to initialSync only: the continuous watch loop's ignore (getWatchRules) never carried this exclusion, and — because it matches against full paths rather than basenames — would need a differently-anchored matcher to work there; that's a separate change if wanted.

Related issue
N/A

(not mandatory) A picture of a cute animal, if possible in relation to what you did

Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
@htoyoda18
htoyoda18 requested review from a team as code owners August 23, 2026 13:49
@htoyoda18
htoyoda18 requested review from glours and ndeloof August 23, 2026 13:49

@glours glours 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.

Hey @htoyoda18
Thanks for this contribution, can you address the few comments I made so we can merge it?

Comment thread pkg/compose/watch.go Outdated

// the Dockerfile and compose files drive the build/orchestration, not the
// application: never copy them into the container on initial sync
dockerFileIgnore, err := watch.NewDockerPatternMatcher("/", []string{"Dockerfile", "*compose*.y*ml"})

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*.y*ml matches any basename containing "compose" plus a .y…ml suffix (e.g. my-compose-notes.yaml, recompose.yml), silently dropping unrelated files from the sync. Consider matching literal names from:
compose-go/v2/cli.DefaultFileNames/DefaultOverrideFileNames.

Comment thread pkg/compose/watch.go Outdated
Comment on lines +788 to +789
// the Dockerfile and compose files drive the build/orchestration, not the
// application: never copy them into the container on initial sync

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.

This restates the existing doc comment on initialSync a few lines above; consider dropping it or shortening it to just the new detail.

Comment thread pkg/compose/watch_test.go Outdated
// matcher nor EphemeralPathMatcher cover this.
func TestInitialSync_ExcludesDockerfileAndComposeFiles(t *testing.T) {
hostDir := t.TempDir()
for _, name := range []string{"Dockerfile", "compose.yaml", "compose.override.yml", "app.go"} {

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.yaml and compose.override.yml both hit the same glob branch with an empty prefix, so the second file adds no coverage.
Swap it for docker-compose.yml instead, it exercises the wildcard matching a non-empty prefix, which thecurrent fixtures don't test.

Comment thread pkg/compose/watch.go Outdated

// the Dockerfile and compose files drive the build/orchestration, not the
// application: never copy them into the container on initial sync
dockerFileIgnore, err := watch.NewDockerPatternMatcher("/", []string{"Dockerfile", "*compose*.y*ml"})

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.

This only excludes the literal Dockerfile, so a custom-named one (build.dockerfile: Dockerfile.prod) still gets copied into the container, the same bug this PR fixes, just for non-default names.
Add service.Build.Dockerfile (nil-checked) to the pattern list.

…and basename

Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
@htoyoda18

Copy link
Copy Markdown
Contributor Author

@glours
Thanks for the review! Addressed all four points:

  • Literal names from cli.DefaultFileNames/DefaultOverrideFileNames
    instead of the *compose*.y*ml glob.
  • Trimmed the comment duplicating initialSync's doc comment.
  • Added docker-compose.yml to the test fixtures to cover the
    non-empty-prefix wildcard case.
  • Excluded service.Build.Dockerfile from the sync, with a test.

While testing the last one, I found it didn't cover a Dockerfile in a
subdirectory (docker/Dockerfile.prod) — the matcher only ever compares
basenames. Fixed with filepath.Base(service.Build.Dockerfile) and added
a regression test.

@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/compose/watch.go 75.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@htoyoda18
htoyoda18 requested a review from glours August 24, 2026 12:43

@glours glours 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.

Thanks for your contribution @htoyoda18

@glours
glours merged commit 8ddbdc4 into docker:main Aug 24, 2026
49 checks passed
@htoyoda18
htoyoda18 deleted the fix/watch-initial-sync-exclude-compose-files branch August 24, 2026 13:37
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.

2 participants