Warnings about unset environment variables even when not used in running containers
Description
Description
The docker compose command outputs warnings about environment variables not being set, even when those variables are only used in services that are not currently being started.
For example, when running docker compose --profile profile1 up or docker compose up service_1, I get the following warning:
WARN[0000] The "BAR" variable is not set. Defaulting to a blank string.
You can use this docker-compose.yml file to reproduce the problem:
services:
service_1:
image: service_1
profiles: [profile1]
service_2:
image: service_2
profiles: [profile2]
environment:
ENV_VAR: ${BAR}
Environment
- Docker Compose version: v2.28.1-desktop.1
- Operating system: macOS 14.4.1 (Darwin 23.4.0 arm64)
This is due to the compose file processing logic: compose.yaml is first parsed and interpolated, then compose select services to run and ignores others. To implement requested feature so that docker compose up service_1 does not trigger interpolation on service_2, the loader would need to be aware about selected services. But then interpolating service_1 might reveal a dependency to service_2 (based on some depends_on or network_mode attribute) and need to also interpolate the target ... So basically we would have to fully redesign the compose-go library for this purpose
Yeah, I thought about that, but I wonder if we could defer variable interpolation until when the services actually run, rather than during the initial load.
we can't as go is a typed language, so interpolation has to take place before we build a compose-go Project from yaml, so that (for illustration) port 80 is mapped to a uint32
Not sure if it helps, but environment variables are always string. https://pkg.go.dev/os#Getenv
There's no short term solution for this, Compose-go does a full compose.yaml parsing and resolution, and as such require all variables to be resolved. Changing this would be a full compose-o/docker-compose redesign, which would require a major version bump. Closing as "not planned" while this is under consideration for future plans.