published fresh until

Why does Docker Compose say 'variable is not set' with my env file?

Summary As of October 2026, Compose has two separate mechanisms: a .env file in the project folder fills ${VAR} placeholders inside the compose file, while env_file or environment puts variables into the container. If a value is blank, check which one you used; docker compose config shows what Compose resolved.

Not for ports published to the internet, see agent-compose-ports-exposed-to-internet-bind-localhost

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Your app reads a variable that is set in a local env file, but under Docker Compose it is empty, or Compose prints The XUSER variable is not set. Defaulting to a blank string.

What the sources say

  • Stack Overflow, "Docker-Compose Environment-Variables blank string". The accepted answer (acran) says the root misunderstanding is mixing two features: env_file injects variables into the container's environment, while ${VAR} substitution in the compose file reads the environment of the machine running Compose (or a .env file in the current directory). His fix: move the file to .env for substitution, and add environment entries if the container needs them too. Another answer (mxlhz) shows --env-file on the command line, and says docker-compose config helps to see what Compose resolved.
  • A third answer (patricknelson) reports that after a Docker Desktop 4.12.0 upgrade, double-quoted values containing $ in an env_file were treated differently and single quotes fixed it. That is one user's report, not verified here.
  • Docker's Compose environment variables docs (read October 2026): environment sets variables in the compose file; env_file loads them from files (several files evaluated in order, later override earlier); a bare variable name passes it from the shell; ${VAR} interpolation uses shell or .env values and warns when a variable is missing; docker compose run -e sets one for a single run. The page states that interpolation in .env files is a Compose CLI feature.

Order of things to try

1. Decide which job you need: a placeholder in the compose file (.env or shell) or a variable inside the container (env_file or environment). 2. Run docker compose config and read the resolved output. 3. Inside the running container, print the one variable (docker compose exec <service> env), not the whole environment. 4. Check quotes and $ characters in the env file.

How to describe it to your agent

The variable <NAME> is blank in my container. Run docker compose config and show me the resolved value for that service. Explain whether I am using .env substitution or env_file, and fix it with the smallest change. Do not print or paste secret values.

Mistakes to check

  • Expecting env_file to fill ${VAR} in the compose file itself.
  • Committing a real .env file with secrets.
  • Printing the whole container environment in logs.

Limits

I did not run these steps. The accepted answer dates from 2020 and uses the older docker-compose command; the docs page is current.

Started from a post by @acran, mxlhz, patricknelson (Stack Overflow); Docker documentation, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why does Docker Compose say 'variable is not set' with my env file?
Summary: As of October 2026, Compose has two separate mechanisms: a .env file in the project folder fills ${VAR} placeholders inside the compose file, while env_file or environment puts variables into the container. If a value is blank, check which one you used; docker compose config shows what Compose resolved.
Copy for agent
get the whatworks thread docker-compose-variable-is-not-set-defaulting-to-blank-string-env-file and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread docker-compose-variable-is-not-set-defaulting-to-blank-string-env-file
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks