admin管理员组

文章数量:1399959

I have a docker compose file and a .env file. Everything is working as intended, except when I run docker compose up I get:

$ docker compose up --build
WARN[0000] The "t" variable is not set. Defaulting to a blank string.
WARN[0000] The "c" variable is not set. Defaulting to a blank string.

There is not t or c environmental variables in my .env file. I don't know where this is coming from. I don't reference them in my docker file or my compose.

added: Here's the compose file:

services:
  db:
    image: postgres:16
    environment:
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    env_file:
      - .env

  django-web:
    build: .
    container_name: django-docker
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      SECRET_KEY: ${SECRET_KEY}
      DEBUG: ${DEBUG}
      DB_NAME: ${DB_NAME}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_USER: ${DB_USER}
      DB_HOST: ${DB_HOST}
      DB_PORT: ${DB_PORT}
      ALLOWED_HOSTS: ${ALLOWED_HOSTS}
    env_file:
      - .env

volumes:
  postgres_data:

The .env file is just a standard file like:

DB_HOST=127.0.0.1
DB_PORT=5432
DB_ENGINE=postgresql_psycopg2

I have a docker compose file and a .env file. Everything is working as intended, except when I run docker compose up I get:

$ docker compose up --build
WARN[0000] The "t" variable is not set. Defaulting to a blank string.
WARN[0000] The "c" variable is not set. Defaulting to a blank string.

There is not t or c environmental variables in my .env file. I don't know where this is coming from. I don't reference them in my docker file or my compose.

added: Here's the compose file:

services:
  db:
    image: postgres:16
    environment:
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    env_file:
      - .env

  django-web:
    build: .
    container_name: django-docker
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      SECRET_KEY: ${SECRET_KEY}
      DEBUG: ${DEBUG}
      DB_NAME: ${DB_NAME}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_USER: ${DB_USER}
      DB_HOST: ${DB_HOST}
      DB_PORT: ${DB_PORT}
      ALLOWED_HOSTS: ${ALLOWED_HOSTS}
    env_file:
      - .env

volumes:
  postgres_data:

The .env file is just a standard file like:

DB_HOST=127.0.0.1
DB_PORT=5432
DB_ENGINE=postgresql_psycopg2
Share Improve this question edited Mar 25 at 14:26 JimNeedsCoffee asked Mar 25 at 12:30 JimNeedsCoffeeJimNeedsCoffee 726 bronze badges 7
  • 1 This usually means you have a reference like $t in the Compose file somewhere; possibly you're missing some bit of quoting. Can you edit the question to include a minimal reproducible example, probably the shortest fragment of Compose YAML and .env file that reproduces the issue? – David Maze Commented Mar 25 at 12:46
  • Shouldn't the env values be between double quotes? "127.0.0.1" and so on – aran Commented Mar 25 at 14:36
  • Does by any chance your DB_PASSWORD or SECRET_KEY (or any other environment variable) contain a $ sign? – derpirscher Commented Mar 25 at 14:39
  • @derpirscher This is it. Fot to check this. The Django secret key does. I guess I need to escape them somehow? – JimNeedsCoffee Commented Mar 25 at 14:42
  • is SECRET_KEY set as environment variable on the host or contained in an .env file? If the latter, enclosing the value with quotes should do the trick – derpirscher Commented Mar 25 at 14:47
 |  Show 2 more comments

2 Answers 2

Reset to default 0

The issue may arise because the value of a variable in .env contains the $ character. You can enclose the value in single quotes (') or double $$ to isolate it properly.


Details

If the variable in .env contains $:

SECRET_KEY=$t!hismight$c!ausetheissue

Using docker compose config to inspect how .env variables are passed into docker-compose.yml

The log shows:

time="2025-03-29T21:12:49+08:00" level=warning msg="The \"t\" variable is not set. Defaulting to a blank string."
time="2025-03-29T21:12:49+08:00" level=warning msg="The \"c\" variable is not set. Defaulting to a blank string."

Both $t and $c in SECRET_KEY are treated as a blank string:

...
django-web:  
    environment:
      SECRET_KEY:'!hismight!ausetheissue'

Fix it by adding single quotes ' or double $$:

SECRET_KEY='$t!hismight$c!ausetheissue'
# or
SECRET_KEY=$$t!hismight$$c!ausetheissue

docker compose config Output as below:

...
django-web:  
    environment:
      SECRET_KEY: $$t!hismight$$c!ausetheissue

Although docker compose config shows $$t!hismight$$c!ausetheissue, when you actually check the variable inside the container, the value is indeed $t!hismight$c!ausetheissue.


Further validation steps

Here are the steps for self-verification based on your example:

  1. In docker-compose.yml, add below for background running:

    services:
      django-web:
      ...
        tty: true
        stdin_open: true
        command: tail -f /dev/null
      ...
    
  2. Mock .env with $ value:

    SECRET_KEY='$t!hismight$c!ausetheissue'
    
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_ENGINE=postgresql_psycopg2
    DB_NAME=test
    DB_USER=test_user
    DB_PASSWORD=test_pwd
    ALLOWED_HOSTS=allowed_user1,allowed_user2 
    DEBUG=True
    
  3. Minimal Dockerfile

    FROM python:3.10-slim-buster
    
    WORKDIR /app
    
  4. Run docker compose up -d --build rebuild image and run container in background

  5. Run docker exec -it django-docker /bin/bash to access container terminal

  6. Check the actual value of SECRET_KEY inside the container's terminal:

    echo $SECRET_KEY
    # $t!hismight$c!ausetheissue
    

Check for Extra .env Files

Docker automatically loads a .env file from the directory where you run docker compose up. Run:

$ ls -la | grep .env 

If you have multiple .env files, one of them might contain t= or c=.

Explicitly Define .env in docker compose Command

Try running:

$ docker compose --env-file .env up --build 

This ensures Docker is using only your specified .env file.

Check the Docker Compose Config Output

Run:

$ docker compose config 

This expands all environment variables and can help you spot if t or c appear unexpectedly.

Let me know what you find!

本文标签: Docker compose warning about environmental variables that don39t existStack Overflow