Docker Compose File cant get .env Variables

Viewed 1168

I am using docker-compose to run a traefik container. The Domain of this Container should be set by an environment file but everytime i start this service it says:

WARNING: The DOMAIN variable is not set. Defaulting to a blank string

My compose-file setup:

version: '3.5'

networks:
  frontend:
    name: frontend
  backend:
    name: backend

services:
  Traefik:
    image: traefik:latest
    command: --api --docker --acme.email="test@test.de"
    restart: always
    container_name: Traefik
    networks:
      - backend
      - frontend
    env_file: ./env.env
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik/traefik.toml:/traefik.toml
      - ./traefik/acme.json:/acme.json
    ports:
      - "80:80"
      - "443:443"
    labels:
      - "traefik.docker.network=frontend"
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:traefik.${DOMAIN}"
      - "traefik.port=8080"
      - "traefik.protocol=http"

My env.env file setup:

DOMAIN=fiture.de

Thanks for your Help!

1 Answers
    env_file: ./env.env

The file env.env isn't loaded to parse the compose file, it is loaded to add environment variables within the container being run. At the point docker processes the above instruction, the yaml file has already been loaded and variables have been expanded.

If you are using docker-compose to deploy containers on a single node, you can rename the file .env and docker-compose will load variables from that file before parsing the compose file.

If you are deploying with docker stack deploy, then you need to import the environment variables into your shell yourself. An example of doing that in bash looks like:

set -a && . ./env.env && set +a && docker stack deploy ...
Related