Skip to content

[BUG] env not being applied to specific service profile #12659

@SayidHosseini

Description

@SayidHosseini

Description

docker compose run <PROFILE_NAME> does not use .env.

Steps To Reproduce

Here is a sample docker-compose.yml:

services:
  migration:
    image: nginx
    env_file: [.env]
    profiles: [migration]
    command: env

And a sample .env:

TEST=True

Here is the output of docker compose config migration:

services:
  migration:
    profiles:
      - migration
    command:
      - env
    env_file:
      - /root/.env
    image: nginx
    networks:
      default: null
networks:
  default:
    name: example_default

Here is the output of docker compose config migration on Docker Compose version v2.33.1:

services:
  migration:
    profiles:
      - migration
    command:
      - env
    environment:
      TEST: "True"
    image: nginx
    networks:
      default: null
networks:
  default:
    name: example_default

Compose Version

Docker Compose version v2.34.0

Docker Environment

Client: Docker Engine - Community
 Version:           28.0.2
 API version:       1.48
 Go version:        go1.23.7
 Git commit:        0442a73
 Built:             Wed Mar 19 14:36:49 2025
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          28.0.2
  API version:      1.48 (minimum version 1.24)
  Go version:       go1.23.7
  Git commit:       bea4de2
  Built:            Wed Mar 19 14:36:49 2025
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.25
  GitCommit:        bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
 runc:
  Version:          1.2.4
  GitCommit:        v1.2.4-0-g6c52b3f
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
root@prod1:~# docker info
Client: Docker Engine - Community
 Version:    28.0.2
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.22.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.34.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 8
  Running: 8
  Paused: 0
  Stopped: 0
 Images: 11
 Server Version: 28.0.2
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
 runc version: v1.2.4-0-g6c52b3f
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.8.0-55-generic
 Operating System: Ubuntu 24.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 15.24GiB
 Name: prod1
 ID: e46ed808-28f1-491e-ad98-df176ea291c6
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

Also If I try to pass env from cmd, I get the following stack trace:

docker compose run --env-from-file .env migration
WARN[0000] Found orphan containers ([root-migration-run-e27a208a083e]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/compose-spec/compose-go/v2/types.MappingWithEquals.OverrideBy(...)
	github.com/compose-spec/compose-go/v2@v2.4.9/types/mapping.go:49
github.com/docker/compose/v2/pkg/compose.applyRunOptions(0xc00065cea0, _, {0xc0003ca600, 0x0, {0x0, 0x0}, {0x7ffdaca50533, 0x9}, {0x0, 0x0, ...}, ...})
	github.com/docker/compose/v2/pkg/compose/run.go:154 +0x613
github.com/docker/compose/v2/pkg/compose.(*composeService).prepareRun(0xc0003f3180, {_, _}, _, {0xc0003ca600, 0x0, {0x0, 0x0}, {0x7ffdaca50533, 0x9}, ...})
	github.com/docker/compose/v2/pkg/compose/run.go:66 +0xea
github.com/docker/compose/v2/pkg/compose.(*composeService).RunOneOffContainer(0xc0003f3180, {_, _}, _, {0xc0003ca600, 0x0, {0x0, 0x0}, {0x7ffdaca50533, 0x9}, ...})
	github.com/docker/compose/v2/pkg/compose/run.go:35 +0x9b
github.com/docker/compose/v2/cmd/compose.runRun({_, _}, {_, _}, _, {0xc0003863c8, {0x7ffdaca50533, 0x9}, {0x0, 0x0, ...}, ...}, ...)
	github.com/docker/compose/v2/cmd/compose/run.go:329 +0xab6
github.com/docker/compose/v2/cmd/compose.runCommand.func2({0x2c6e960, 0xc0003b2e10}, {0xc000487bf0?, 0xc000006380?, 0x215dc2a?})
	github.com/docker/compose/v2/cmd/compose/run.go:197 +0x299
github.com/docker/compose/v2/cmd/compose.runCommand.Adapt.func5({0x2c6e960?, 0xc0003b2e10?}, 0x2?, {0xc0004d8cf0?, 0x2c52d88?, 0x1e4d3d9?})
	github.com/docker/compose/v2/cmd/compose/compose.go:138 +0x30
github.com/docker/compose/v2/cmd/compose.runCommand.Adapt.AdaptCmd.func7(0xc0001f9508, {0xc0004d8cf0, 0x1, 0x3})
	github.com/docker/compose/v2/cmd/compose/compose.go:122 +0x143
github.com/docker/cli/cli-plugins/plugin.RunPlugin.func1.1.2(0xc0001f9508, {0xc0004d8cf0, 0x1, 0x3})
	github.com/docker/cli@v28.0.1+incompatible/cli-plugins/plugin/plugin.go:65 +0x6c
github.com/docker/compose/v2/cmd/cmdtrace.Setup.wrapRunE.func2(0xc0001f9508?, {0xc0004d8cf0?, 0x1?, 0x3?})
	github.com/docker/compose/v2/cmd/cmdtrace/cmd_span.go:85 +0x63
github.com/spf13/cobra.(*Command).execute(0xc0001f9508, {0xc0003b2b60, 0x3, 0x3})
	github.com/spf13/cobra@v1.9.1/command.go:1015 +0xa94
github.com/spf13/cobra.(*Command).ExecuteC(0xc0004daf08)
	github.com/spf13/cobra@v1.9.1/command.go:1148 +0x40c
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.9.1/command.go:1071
github.com/docker/cli/cli-plugins/plugin.RunPlugin(0xc0002cfb80, 0xc00059e908, {{0x2876b88, 0x5}, {0x2880cab, 0xb}, {0x2c3cdd0, 0x7}, {0x0, 0x0}, ...})
	github.com/docker/cli@v28.0.1+incompatible/cli-plugins/plugin/plugin.go:80 +0x145
github.com/docker/cli/cli-plugins/plugin.Run(0x29d7400, {{0x2876b88, 0x5}, {0x2880cab, 0xb}, {0x2c3cdd0, 0x7}, {0x0, 0x0}, {0x0, ...}})
	github.com/docker/cli@v28.0.1+incompatible/cli-plugins/plugin/plugin.go:95 +0x105
main.pluginMain()
	github.com/docker/compose/v2/cmd/main.go:38 +0xa5
main.main()
	github.com/docker/compose/v2/cmd/main.go:98 +0x19c
docker compose run --env TEST=True  migration
WARN[0000] Found orphan containers ([root-migration-run-e27a208a083e]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/compose-spec/compose-go/v2/types.MappingWithEquals.OverrideBy(...)
	github.com/compose-spec/compose-go/v2@v2.4.9/types/mapping.go:49
github.com/docker/compose/v2/pkg/compose.applyRunOptions(0xc0006bb5f0, _, {0xc00044a080, 0x0, {0x0, 0x0}, {0x7ffd89179533, 0x9}, {0x0, 0x0, ...}, ...})
	github.com/docker/compose/v2/pkg/compose/run.go:154 +0x613
github.com/docker/compose/v2/pkg/compose.(*composeService).prepareRun(0xc000540c80, {_, _}, _, {0xc00044a080, 0x0, {0x0, 0x0}, {0x7ffd89179533, 0x9}, ...})
	github.com/docker/compose/v2/pkg/compose/run.go:66 +0xea
github.com/docker/compose/v2/pkg/compose.(*composeService).RunOneOffContainer(0xc000540c80, {_, _}, _, {0xc00044a080, 0x0, {0x0, 0x0}, {0x7ffd89179533, 0x9}, ...})
	github.com/docker/compose/v2/pkg/compose/run.go:35 +0x9b
github.com/docker/compose/v2/cmd/compose.runRun({_, _}, {_, _}, _, {0xc00051c718, {0x7ffd89179533, 0x9}, {0x0, 0x0, ...}, ...}, ...)
	github.com/docker/compose/v2/cmd/compose/run.go:329 +0xab6
github.com/docker/compose/v2/cmd/compose.runCommand.func2({0x2c6e960, 0xc0003f7090}, {0xc000012180?, 0xc000006380?, 0x215dc2a?})
	github.com/docker/compose/v2/cmd/compose/run.go:197 +0x299
github.com/docker/compose/v2/cmd/compose.runCommand.Adapt.func5({0x2c6e960?, 0xc0003f7090?}, 0x2?, {0xc000439470?, 0x2c52d88?, 0x2f078f3?})
	github.com/docker/compose/v2/cmd/compose/compose.go:138 +0x30
github.com/docker/compose/v2/cmd/compose.runCommand.Adapt.AdaptCmd.func7(0xc0001f9b08, {0xc000439470, 0x1, 0x3})
	github.com/docker/compose/v2/cmd/compose/compose.go:122 +0x143
github.com/docker/cli/cli-plugins/plugin.RunPlugin.func1.1.2(0xc0001f9b08, {0xc000439470, 0x1, 0x3})
	github.com/docker/cli@v28.0.1+incompatible/cli-plugins/plugin/plugin.go:65 +0x6c
github.com/docker/compose/v2/cmd/cmdtrace.Setup.wrapRunE.func2(0xc0001f9b08?, {0xc000439470?, 0x1?, 0x3?})
	github.com/docker/compose/v2/cmd/cmdtrace/cmd_span.go:85 +0x63
github.com/spf13/cobra.(*Command).execute(0xc0001f9b08, {0xc0003f6c00, 0x3, 0x3})
	github.com/spf13/cobra@v1.9.1/command.go:1015 +0xa94
github.com/spf13/cobra.(*Command).ExecuteC(0xc0004b5208)
	github.com/spf13/cobra@v1.9.1/command.go:1148 +0x40c
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.9.1/command.go:1071
github.com/docker/cli/cli-plugins/plugin.RunPlugin(0xc000451680, 0xc00014b208, {{0x2876b88, 0x5}, {0x2880cab, 0xb}, {0x2c3cdd0, 0x7}, {0x0, 0x0}, ...})
	github.com/docker/cli@v28.0.1+incompatible/cli-plugins/plugin/plugin.go:80 +0x145
github.com/docker/cli/cli-plugins/plugin.Run(0x29d7400, {{0x2876b88, 0x5}, {0x2880cab, 0xb}, {0x2c3cdd0, 0x7}, {0x0, 0x0}, {0x0, ...}})
	github.com/docker/cli@v28.0.1+incompatible/cli-plugins/plugin/plugin.go:95 +0x105
main.pluginMain()
	github.com/docker/compose/v2/cmd/main.go:38 +0xa5
main.main()
	github.com/docker/compose/v2/cmd/main.go:98 +0x19c

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions