Repository: docker/compose

Issue:
[BUG] `env_file` is not loaded in `docker compose` (v2.34.0) for services with profiles
### Description

In Docker Compose version 2.34.0, environment variables defined in an `env_file` are no longer loaded when running a service using `docker compose run <service name>`, specifically for services that have `profiles:`, **if** profile is not enabled explicitly via `COMPOSE_PROFILES` or `--profile`. In version 2.33.1, the variables were correctly loaded.

### Steps to reproduce

1. Create a `docker-compose.yaml` file:

   ```yaml
   services:
     service-with-profile:
       profiles: [ test-profile ]
       env_file: .env
       command: bash -c "env | grep TEST_VAR"
       image: ubuntu:24.04
   ```

2. Create a `.env` file:

   ```
   TEST_VAR_1=1
   TEST_VAR_2=2
   ```

3. Run the service using `docker compose run`:

   ```sh
   docker compose run --rm service-with-profile
   ```

**Expected Behavior (Docker Compose v2.33.1 or if `COMPOSE_PROFILES` / `--profile` is provided):**

```
TEST_VAR_2=2
TEST_VAR_1=1
```

**Actual Behavior (Docker Compose v2.34.0):**

```
(no output, environment variables are missing)
```

### System Information

```
$ docker version
Client:
 Version:           28.0.1
 API version:       1.48
 Go version:        go1.24.1
 Git commit:        068a01ea94
 Built:             Thu Mar  6 19:09:48 2025
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          28.0.1
  API version:      1.48 (minimum version 1.24)
  Go version:       go1.24.1
  Git commit:       bbd0a17ccc
  Built:            Thu Mar  6 19:09:48 2025
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v2.0.3
  GitCommit:        06b99ca80cdbfbc6cc8bd567021738c9af2b36ce.m
 runc:
  Version:          1.2.5
  GitCommit:        
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
```

```
$ docker compose version  # version with the issue          
Docker Compose version 2.34.0
```

```
$ docker compose version  # working version          
Docker Compose version 2.33.1
```

Discussion:
@codingjerk: @ndeloof Issue was probably introduced [here](https://github.com/docker/compose/commit/b23728941d202749fa8918cf5e8a59d1fe32513c)@lily-controlzee: thanks for reporting. I just encountered the same issue and you saved me the trouble of making a repro@Aden-Q: The issue still exists as of `v2.34.0`, compose doesn't respect `env_file` when there's a `profile` section@davidrunger: > The issue still exists as of `v2.34.0`, compose doesn't respect `env_file` when there's a `profile` section

@Aden-Q : I believe that is expected. I think `v2.34.0` is the release in which this bug was first introduced. Presumably it will be fixed in the next release.@StefanWetterActiware: Does anyone know when a release fixing this bug will be available?@aukaost: Getting a release out for this issue would be most appreciated.@davidrunger: [v2.35.0](https://github.com/docker/compose/releases/tag/v2.35.0) is now available, which includes a fix for this bug.

Solution patch:
diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go
index 450bb165..7f83bf8a 100644
--- a/cmd/compose/compose.go
+++ b/cmd/compose/compose.go
@@ -170,7 +170,7 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
 	return Adapt(func(ctx context.Context, args []string) error {
 		options := []cli.ProjectOptionsFn{
 			cli.WithResolvedPaths(true),
-			cli.WithDiscardEnvFile,
+			cli.WithoutEnvironmentResolution,
 		}
 
 		project, metrics, err := o.ToProject(ctx, dockerCli, args, options...)
@@ -180,6 +180,11 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
 
 		ctx = context.WithValue(ctx, tracing.MetricsKey{}, metrics)
 
+		project, err = project.WithServicesEnvironmentResolved(true)
+		if err != nil {
+			return err
+		}
+
 		return fn(ctx, project, args)
 	})
 }
diff --git a/go.mod b/go.mod
index b4f13478..5993f95d 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,7 @@ require (
 	github.com/Microsoft/go-winio v0.6.2
 	github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
 	github.com/buger/goterm v1.0.4
-	github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef
+	github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c
 	github.com/containerd/containerd/v2 v2.0.3
 	github.com/containerd/platforms v1.0.0-rc.1
 	github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
diff --git a/go.sum b/go.sum
index 5f32c628..bc6acc34 100644
--- a/go.sum
+++ b/go.sum
@@ -89,8 +89,8 @@ github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004 h1:lkAMpLVBDaj17e
 github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA=
 github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
 github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
-github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef h1:1PcqLEYTxUY28/oZCxtkkF4SpleCrO+Vckw8Skpymz4=
-github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef/go.mod h1:6k5l/0TxCg0/2uLEhRVEsoBWBprS2uvZi32J7xub3lo=
+github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c h1:2KFMBiATRqXYmm/6U1Y3WUxorVBERkdwM1ax7vJI2OQ=
+github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c/go.mod h1:6k5l/0TxCg0/2uLEhRVEsoBWBprS2uvZi32J7xub3lo=
 github.com/containerd/cgroups/v3 v3.0.5 h1:44na7Ud+VwyE7LIoJ8JTNQOa549a8543BmzaJHo6Bzo=
 github.com/containerd/cgroups/v3 v3.0.5/go.mod h1:SA5DLYnXO8pTGYiAHXz94qvLQTKfVM5GEVisn4jpins=
 github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
diff --git a/pkg/e2e/fixtures/env_file/compose.yaml b/pkg/e2e/fixtures/env_file/compose.yaml
new file mode 100644
index 00000000..07d5dfeb
--- /dev/null
+++ b/pkg/e2e/fixtures/env_file/compose.yaml
@@ -0,0 +1,8 @@
+services:
+  serviceA:
+    image: nginx:latest
+
+  serviceB:
+    image: nginx:latest
+    env_file:
+      - /doesnotexist/.env
\ No newline at end of file


Test patch:
diff --git a/pkg/e2e/env_file_test.go b/pkg/e2e/env_file_test.go
index 5b546764..ed7c8431 100644
--- a/pkg/e2e/env_file_test.go
+++ b/pkg/e2e/env_file_test.go
@@ -30,3 +30,10 @@ func TestRawEnvFile(t *testing.T) {
 	res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dotenv/raw.yaml", "run", "test")
 	assert.Equal(t, strings.TrimSpace(res.Stdout()), "'{\"key\": \"value\"}'")
 }
+
+func TestUnusedMissingEnvFile(t *testing.T) {
+	c := NewParallelCLI(t)
+	defer c.cleanupWithDown(t, "unused_dotenv")
+
+	c.RunDockerComposeCmd(t, "-f", "./fixtures/env_file/compose.yaml", "up", "-d", "serviceA")
+}