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
|
Show 2 more comments
2 Answers
Reset to default 0The 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:
In
docker-compose.yml
, add below for background running:services: django-web: ... tty: true stdin_open: true command: tail -f /dev/null ...
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
Minimal Dockerfile
FROM python:3.10-slim-buster WORKDIR /app
Run
docker compose up -d --build
rebuild image and run container in backgroundRun
docker exec -it django-docker /bin/bash
to access container terminalCheck 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
版权声明:本文标题:Docker compose warning about environmental variables that don't exist - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744196309a2594757.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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:46DB_PASSWORD
orSECRET_KEY
(or any other environment variable) contain a$
sign? – derpirscher Commented Mar 25 at 14:39SECRET_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