In the above example, I'm concerned that perhaps the declared variables being the the same is an issue? ie: MYDOMAIN=${DOMAIN} better than DOMAIN=${DOMAIN} ?
I have not been able to get .env variables working in either my static (traefik.yml) or dynamic configs (dynamic_config.yml).
I'm not sure what I'm doing wrong, but I'm beating my head agains a wall trying to get .env variables in my static and dynamic config files. What is the correct way to format and method to insert env variables to these files? Also, does v3 do this different from v2? Please help! I've attempted read the documentation on this but I'm quite confused about it. Perhaps when I get this working I have a question about secrets too.
So, I guess my confusion is how are the environment variables declared? In the attached link example... they're doing...
docker-compose.yml
...
environment:
# WAN_HOSTNAME is used in traefik config.yml file !
- WAN_HOSTNAME
However, I've seen conflicting forum ideas on how this is done, I've seen it done this way (above), then seen that others say you need to 'declare' (below), linked post suggests the below is incorrect?:
docker-compose.yml
...
environment:
# WAN_HOSTNAME is used in traefik config.yml file !
- WAN_HOSTNAME=${WAN_HOSTNAME}
Am I understanding this correctly?
It also seems this NEEDS to match an exact variable named WAN_HOSTNAME in the .env file? ie from
You declare the environment variables in the .env file and reference them in the compose file so the container can use them in the dynamic config files. I just tested both ways you referenced for docker-compose.yml and both work. However you pass the variable through the compose file - WAN_HOSTNAME= is how you will reference it in Traefik's dynamic config file.
.env
WAN_HOSTNAME='example.com'
ANOTHER_VARIABLE='foo'
docker-compose.yml - WAN_HOSTNAME would be referenced in dynamic_config.yml as {{env "WAN_HOSTNAME"}} - WAN_HOSTNAME=${WAN_HOSTNAME} would be referenced in dynamic_config.yml as {{env "WAN_HOSTNAME"}} - HOSTNAME=${WAN_HOSTNAME} would be referenced in dynamic_config.yml as {{env "HOSTNAME"}}
All would result in the same value of example.com from the .env file.
Here's an example of my setup:
.env
...
## Host IP Address
HOST_IP=192.168.0.10
## Domain Name
DOMAIN=mydomain.com
...
My solution to this was using environment settings for my traefik static config in the traefik compose.yaml. this way I could use .env variables with ${variable}. I can post an example later when at my PC if you want.
One note, is you can't mix static config implementations. Meaning choose one of traefik.yaml or compose environment or cli arguments.
Read: Traefik Configuration Documentation - Traefik