Setting up global environmental variable

Greetings Forum :wave:
I want to set global environmental variables that I can later use in my Custom Apps configuration like with this work-in-progress Traefik docker-compose config:

services:
  traefik:
    image: "traefik:3.2"
    container_name: "traefik"
    ports:
      - "80:80"
      - "443:443"
    restart: unless-stopped
    volumes:
      - ./traefik/config:/etc/traefik
      - ./traefik/certs:/etc/traefik/certs
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik/log:/var/log/traefik
    labels:
      traefik.enable: true
      traefik.http.routers.traefik-api.rule: "Host(`traefik.${DOMAIN}`)"
      traefik.http.routers.traefik-api.entryPoints: "web, websecure"
      traefik.http.routers.traefik-api.tls: "true"
      traefik.http.routers.traefik-api.tls.certresolver: "cloudflare"
      traefik.http.routers.traefik-api.tls.domains[0].main: "${DOMAIN}"
      traefik.http.routers.traefik-api.tls.domains[0].sans: "*.${DOMAIN}"
      traefik.http.routers.traefik-api.service: "api@internal"
      traefik.http.routers.traefik-api.middlewares: "basic-auth@file"  # Defined in traefik/config/middlewares.yml
    environment:
      - CF_API_EMAIL=email.site.com
      - CF_API_KEY=${CF_API_KEY}
    networks:
      - traefik-proxy

The main point is that the ${DOMAIN} should be available from env. I’ve tried to achieve this by setting up the values in /etc/profile and /etc/environment or the init startup scripts from the UI, but with no success. Have anyone tried to do something like this?

I am no docker expert. Don’t you need something like this in your compose file?

env_file:
  /path/.env

Unfortunately, env_file would work. It’s used to pass environment variables to the containers themselves, whereas I need the environment variables set before TrueNas executes my yaml.

I am still upgrading from Core to Scale, so need to move to Docker later this week (first changing ACLs for days) and am a beginner with Docker.

What I would do is create a dataset, like /mnt/tank/dockerdata/ and then create a file global.env with your global variables in it.
Every compose file can load this file too, env_file can refer to multiple files.
Where am I wrong in my thought process?

Or perhaps a startup script (using REGEXs?) to put a domain in a memory variable? (way out of my league for now :slight_smile: )

So the issue is that I don’t have a problem passing environmental variables to the containers, I need to read values from environmental values during docker compose up execution command that will be used as an argument for the labels for the containers. The environment values should be already present at the custom application creation step, since they cannot be loaded from a file like with env_file .

I was gonna link the doc about the env_file as well as docker compose interpolations but I couldn’t :smiley:
reference/compose-file/services/#env_file
reference/compose-file/interpolation/

I was able to launch my own custom containers from cli, but they didn’t show up in the applications tab. My assumption is that this Apps tab queries some external state and doesn’t monitor other docker containers.

1 Like

Thanks learned something.

Have you tried Dockge (or Portainer)?

So I was able to directly spin up custom containers with exporting the env variables first, so like

export DOMAIN=test.domain.com
docker compose up -d

But those containers don’t show up on the Apps page, my guess is that TrueNAS stores them somewhere in their own state/database. Maybe there is a way to make them appear on the UI?

Did you try if they show up in Dockge e.g.?
Also, I wonder why you want to use the CLI and not use Custom App in de GUI to start them? (the three dots next to Custom App, shows yaml).

(I am not there yet to test/experiment with it, as I am changing permissions on many many files, taking multiple days after my migration from core to scale)

Because the GUI doesn’t support reusable variables. Instead of changing the value between all the servers and all of the services inside of them, I want to have a mechanism like an Env variable that I can set and forget and have all of my services rely on that value.

The issue is that the core OS functionality of setting the Env variable system-wide on startup doesn’t work in this highly modified “distro”.

You are talking about /etc/profile and /etc/environment - neither are files for “system-wide env variables”. These are files for login shells and pam plugins such as pam_env. Neither of those will affect systemd services such as middlerwared. This is normal behaviour, don’t know of any distribution that works differently.

You want resuable variables in the GUI? File a Feature Request.

1 Like

Vote for this feature request :wink: