Requesting certificate via DNS-01 fails with "some credentials information are missing"

I am trying to setup Traefik 3.7 with ACME DNS-01 Challenge. I have specified the HETZNER_API_TOKEN environment variable in Portainer for the Traefik stack but I keep getting this error message:

Unable to obtain ACME certificate for domains error="cannot get ACME client hetzner: some credentials information are missing: HETZNER_API_TOKEN"

I double checked the name of the variable and the content and they do work when I use them directly with lego.

What am I doing wrong?

Seems like the env var is not correctly passed into the container.

Do you define the variable in the compose file?

services:
  traefik:
    image: traefik:v3.7
    environment:
      - HETZNER_API_TOKEN=${HETZNER_API_TOKEN}

If I define the variable in the compose file, it works. Shouldn't both ways be equivalent?

I think just by defining a variable in Portainer it is not automatically injected into a container.

I have never had any similar issues and I use this way of defining the variables in a number of stacks. They are the equivalent of .env files.

Did you check the official FAQs?

I did now, but I still don't understand why my paperless-ngx stack works with environment variables that I set in the Advanced Container Settings section of the stack configuration while Traefik ignores them.
I have a couple of them defined in my paperless-ngx stack and they are only defined there and not in the compose file but the container still sees them and reacts according to them.