docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.15k stars 5.13k forks source link

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

Open felipelima-circle opened 1 week ago

felipelima-circle commented 1 week ago

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

ndeloof commented 1 week ago

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

felipelima-circle commented 1 week ago

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.

ndeloof commented 1 week ago

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

felipelima-circle commented 1 week ago

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