Skip to content

Fix: respect required: false for env_file in publish command#13652

Open
maks2134 wants to merge 2 commits intodocker:mainfrom
maks2134:fix/env-file-required-false-final
Open

Fix: respect required: false for env_file in publish command#13652
maks2134 wants to merge 2 commits intodocker:mainfrom
maks2134:fix/env-file-required-false-final

Conversation

@maks2134
Copy link
Copy Markdown

What I did
Fixed docker compose publish command to respect required: false setting on env_file entries. Previously, the command would fail when optional env files were missing, even when explicitly marked as not required.

Changes made:

  • Modified checkForSensitiveData() to skip env files with required: false
  • Updated processFile() to only process required env files into publish layers
  • Fixed checkEnvironmentVariables() to only consider required env files for environment warnings
  • Added comprehensive tests to verify the fix works correctly

Related issue
fixes #13648

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

Fixes docker#13648

The docker compose publish command was ignoring required: false
setting on env_file entries, causing failures when optional env files
were missing.

Changes made:
- Modified checkForSensitiveData() to skip env files with required: false
- Updated processFile() to only process required env files into layers
- Fixed checkEnvironmentVariables() to only consider required env files
- Added comprehensive tests to verify the fix

Signed-off-by: Maks Kozlov <maks@example.com>
Signed-off-by: maks2134 <maks210306@yandex.by>
@maks2134 maks2134 requested a review from a team as a code owner March 20, 2026 20:54
@maks2134 maks2134 requested review from glours and ndeloof March 20, 2026 20:54
ndeloof
ndeloof previously approved these changes Mar 21, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 21, 2026

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/publish.go 75.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

@glours glours left a comment

Choose a reason for hiding this comment

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

IMHO, we should allow non-required env files, check the sensitive data and add them as a layer when present on the user’s host.

Comment on lines +254 to +256
if !envFile.Required {
continue
}
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.

If the file does exist on the user's host, it should still be published as a layer and skipped by default

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

Comment on lines +447 to +449
if !envFile.Required {
continue
}
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.

Same here, we should check env variables if the file exist on user's host

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

- Process optional env files if they exist on disk (instead of skipping all)
- Improve error message to mention "required env_file" for clarity
- Add test coverage for optional env files that exist
- Fix linting issues

Signed-off-by: Maks Kozlov <maks@example.com>
Signed-off-by: maks2134 <maks210306@yandex.by>
@maks2134
Copy link
Copy Markdown
Author

Please take a look at the changes when you have the opportunity. @glours

Copy link
Copy Markdown
Contributor

@glours glours left a comment

Choose a reason for hiding this comment

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

This is still not the right expect behaviour, we should check the env files content if the files exist even if there are not mandatory

Comment on lines +359 to +363
for _, envFile := range service.EnvFiles {
if envFile.Required {
errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has required env_file declared.", service.Name))
break
}
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.

We should also return an error when the file is not required but present on the user disk, because it will be upload as part of the Compose artifact


err := service.checkEnvironmentVariables(project, api.PublishOptions{})
assert.Assert(t, err != nil)
assert.Assert(t, strings.Contains(err.Error(), `service "test" has required env_file declared.`))
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.

nit

Suggested change
assert.Assert(t, strings.Contains(err.Error(), `service "test" has required env_file declared.`))
assert.ErrorContains(t, err, `service "test" has required env_file declared.`)

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 publish ignores required: false on env_file

3 participants