Commands like `docker compose ps` do not read `.env` File

When running docker compose ps or docker-compose top on a file with lots of environment variables, expanded via a local .env file on build, the linting of the file fails like this.

error while interpolating services.app.read_only: failed to cast to expected type: invalid boolean:

The raw value is a string, because it expands to true or false on build.

Is there a way to avoid or fix this?

Should this be a Github issue?

It seems that the value of the read_only variable is a string that expands to either true or false on build. However, Docker Compose is expecting a boolean value, not a string.

To fix this issue, you can explicitly cast the read_only value to a boolean in your Docker Compose file using the !!bool tag. For example:

services:
  app:
    environment:
      - READ_ONLY: !!bool ${READ_ONLY}

This will ensure that the READ_ONLY environment variable is interpreted as a boolean, regardless of its value.

Alternatively, you can modify the format of the environment variable to use the YAML boolean format instead of a string that expands to a boolean. For example:

services:
  app:
    environment:
      - READ_ONLY: true

Thanks for the trick, seems noteworthy.

However, in my case it does not help much. Apparently, using this outside of the environment section, the behaviour & expectations are different. :confused:

Source

services:
  app:
    read_only: ${READ_ONLY}

Output

error while interpolating services.app.read_only: failed to cast to expected type: invalid boolean

Source

services:
  app:
    read_only: !!bool ${READ_ONLY}

Output

yaml: cannot decode !!str `${READ_ONLY}` as a !!bool

What is the version of Docker Compose? In case of an empty variable the error message should look like this:

error while interpolating services.app.read_only: failed to cast to expected type: invalid boolean:

Did you have a “:” character at the end originally?

update:

Sorry, I see you had in your first post. But I would like to know the version of your compose still :slight_smile: