compose icon indicating copy to clipboard operation
compose copied to clipboard

Warnings about unset environment variables even when not used in running containers

Open felipelima-circle opened this issue 1 year ago • 4 comments

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)

felipelima-circle avatar Jul 09 '24 00:07 felipelima-circle

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

ndeloof avatar Jul 09 '24 08:07 ndeloof

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.

felipelima-circle avatar Jul 09 '24 21:07 felipelima-circle

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

ndeloof avatar Jul 10 '24 05:07 ndeloof

Not sure if it helps, but environment variables are always string. https://pkg.go.dev/os#Getenv

felipelima-circle avatar Jul 12 '24 03:07 felipelima-circle

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.

ndeloof avatar Oct 15 '24 09:10 ndeloof