Skip to content

Conversation

crazy-max
Copy link
Member

fixes #3288

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
filteredServices[svcName] = map[string]any{}
} else if svcMap, ok := svc.(map[string]any); ok {
filteredService := make(map[string]any)
for _, svcField := range []string{"image", "build", "environment", "env_file"} {
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT you don't need environment or env_file as those won't have any impact on the build context (they only apply on runtime environment)

Copy link
Member Author

Choose a reason for hiding this comment

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

We need them to evaluate build arguments:

buildx/bake/compose_test.go

Lines 396 to 424 in a007368

func TestEnv(t *testing.T) {
envf, err := os.CreateTemp("", "env")
require.NoError(t, err)
defer os.Remove(envf.Name())
_, err = envf.WriteString("FOO=bsdf -csdf\n")
require.NoError(t, err)
dt := []byte(`
services:
scratch:
build:
context: .
args:
CT_ECR: foo
FOO:
NODE_ENV:
environment:
- NODE_ENV=test
- AWS_ACCESS_KEY_ID=dummy
- AWS_SECRET_ACCESS_KEY=dummy
env_file:
- ` + envf.Name() + `
`)
c, err := ParseCompose([]composetypes.ConfigFile{{Content: dt}}, nil)
require.NoError(t, err)
require.Equal(t, map[string]*string{"CT_ECR": ptrstr("foo"), "FOO": ptrstr("bsdf -csdf"), "NODE_ENV": ptrstr("test")}, c.Targets[0].Args)
}

If not set then this test fails:

        	Error:      	Not equal: 
        	            	expected: map[string]*string{"CT_ECR":(*string)(0xc00060a510), "FOO":(*string)(0xc00060a520), "NODE_ENV":(*string)(0xc00060a530)}
        	            	actual  : map[string]*string{"CT_ECR":(*string)(0xc00060a4a0)}
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,5 +1,3 @@
        	            	-(map[string]*string) (len=3) {
        	            	- (string) (len=6) "CT_ECR": (*string)((len=3) "foo"),
        	            	- (string) (len=3) "FOO": (*string)((len=10) "bsdf -csdf"),
        	            	- (string) (len=8) "NODE_ENV": (*string)((len=4) "test")
        	            	+(map[string]*string) (len=1) {
        	            	+ (string) (len=6) "CT_ECR": (*string)((len=3) "foo")
        	            	 }
        	Test:       	TestEnv
--- FAIL: TestEnv (0.00s)

Copy link
Contributor

Choose a reason for hiding this comment

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

There's a bug then. Runtime environment MUST not impact build environment; Sounds there's a confusion here with client environment (which is defined by .env file and --env-file flag in Compose) ... which is unfortunately a pretty common source of confusion 😭

Copy link
Member Author

Choose a reason for hiding this comment

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

Argh yes seems it was a mistake, this was introduced in #905. What about .env then?

Copy link
Contributor

Choose a reason for hiding this comment

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

For parity with Compose, .env should be loaded (from compose file's parent folder), and interpolated using os.Env


return loader.ModelToProject(filtered, loader.ToOptions(&cfgDetails, append([]func(*loader.Options){func(options *loader.Options) {
options.SkipNormalization = true
options.Profiles = []string{"*"}
Copy link
Contributor

Choose a reason for hiding this comment

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

building with all profiles enabled is not consistent with Compose. Is this expected ?

Copy link
Member Author

Choose a reason for hiding this comment

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

We allow all profiles because Bake doesn't support them: #1903. If we add something similar in our spec I think we could reconsider that. cc @colinhemmings

Copy link
Member

Choose a reason for hiding this comment

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

In follow-up we could consider mapping compose profiles to bake groups and maybe defining a default profile that would be used if one is defined.

@crazy-max crazy-max force-pushed the compose-filtered-spec branch from 05451de to 0ce459f Compare July 3, 2025 16:08
Environment: envs,
}

raw, err := loader.LoadModelWithContext(context.Background(), cfgDetails, append([]func(*loader.Options){func(opts *loader.Options) {
Copy link
Member

Choose a reason for hiding this comment

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

We can do this as follow-up as not directly related but seems fine to pass in the right context from the command.


return loader.ModelToProject(filtered, loader.ToOptions(&cfgDetails, append([]func(*loader.Options){func(options *loader.Options) {
options.SkipNormalization = true
options.Profiles = []string{"*"}
Copy link
Member

Choose a reason for hiding this comment

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

In follow-up we could consider mapping compose profiles to bake groups and maybe defining a default profile that would be used if one is defined.

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@tonistiigi tonistiigi merged commit f6d2c5e into docker:master Jul 14, 2025
138 checks passed
@crazy-max crazy-max deleted the compose-filtered-spec branch July 14, 2025 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bake: only validate build property in compose file services
3 participants