YAML using incorrect type taken from .env file

Hello, I am attempting to set up a docker-compose.yml file that reads in some environment variables from a .env file. The docker-compose.yml is able to read the .env and it works for all sections except one.

I am attempting to create a container that makes use of an NVIDIA GPU, but make it work whether or not there is one. In the .env, I have a line:


I want to be able to specify in the .env if if the container should use ‘0’ GPUs or ‘all’ GPUs since this container needs to be able to run on some systems that have an NVIDIA GPU and on some systems that don’t.

Here is the snippet from the docker-compose.yml:

                - driver: nvidia
                  count: ${GPU}
                  capabilities: [gpu]
    - ./.env

The ‘count’ option allows for the string “all” or it allows for an invariant integer, which we would want to be 0.

The issue I am having is that when the YAML file attempts to read the environment variables, it will read in the ‘0’ that GPU is set to in the .env, but won’t cast it to an integer. The ‘count’ line will see the variable is equal to “0” instead of the invariant 0 and won’t allow the container to start since the only allowed string count can take is ‘all’.

Is there a way to cast an environment variable to an invariant integer without building up the .yml dynamically?

Thank you