Docker Error Guide: 'env file not found' in Docker Compose
Fix Docker Compose's 'env file .env not found' / 'Couldn't find env file' error: diagnose missing or mispathed .env files, env_file: paths, and --env-file overrides.
- #docker
- #troubleshooting
- #errors
- #compose
Fixing errors like this? Get 500 free DevOps AI prompts
500 copy-paste AI prompts for the stack you actually run — one PDF, free.
Exact Error Message
env file /home/deploy/app/.env not found: stat /home/deploy/app/.env: no such file or directory
You may also see the Compose V1 wording Couldn't find env file: /home/deploy/app/.env, the service-level form env file ./config/prod.env not found, or failed to read ... : no such file or directory. All of them mean Docker Compose was told to load an environment file that does not exist at the resolved path.
What It Means
Docker Compose reads environment variables from two distinct places. The top-level project .env file (used for variable substitution inside compose.yaml) is loaded from the directory that contains the Compose file, or from the path given by --env-file. Separately, each service can declare an env_file: key pointing at one or more files whose contents become that container’s environment.
The env file ... not found error means one of those referenced paths does not resolve to a real file. Compose does not silently ignore a missing env file; it stops with an error so you do not accidentally start a container with no configuration. The fix is almost always a path or working-directory mismatch.
Common Causes
- A
.envfile is expected but absent (never created, git-ignored, or not copied to the deploy host). env_file:uses a path relative to the Compose file, but you randocker composefrom a different directory than expected.--env-file prod.envwas passed but the file lives elsewhere or is misspelled.- The file exists but with the wrong name (
.env.productionvs.env, or a stray extension). - A CI checkout excludes
.env(correctly ignored) without providing a replacement. - Windows/Unix path separators or a leading
./were confused across environments.
Diagnostic Commands
Confirm which directory Compose treats as the project root and which env file it loads:
docker compose config
List the files actually present where Compose is looking:
ls -la
ls -la config/
Render the resolved configuration to see how variables and paths expand:
docker compose --env-file prod.env config
Check the env_file: entries declared in the Compose file:
grep -n "env_file" compose.yaml
Step-by-Step Resolution
- Read the error’s absolute path. It tells you exactly where Compose looked, for example
/home/deploy/app/.env. Check whether a file exists there:
ls -la /home/deploy/app/.env
- If the file is simply missing, create it (or copy from a template) at that path:
cp .env.example .env
- If the path is wrong, fix the
env_file:entry. Paths are relative to the Compose file’s directory, so be explicit:
services:
web:
image: myapp:latest
env_file:
- ./config/prod.env
- If you pass
--env-file, give the correct location and run Compose from a stable directory:
docker compose --env-file ./deploy/prod.env up -d
- To make a referenced env file optional (Compose v2.24+), mark it
required: falseso a missing file does not abort:
services:
web:
env_file:
- path: ./config/local.env
required: false
- In CI, generate the env file before running Compose, since
.envis usually git-ignored:
printf "DB_HOST=%s\nDB_PASS=%s\n" "$DB_HOST" "$DB_PASS" > .env
docker compose up -d
- Re-run and confirm the stack starts:
✔ Container app-web-1 Started
Prevention
- Commit a
.env.exampletemplate and document thecp .env.example .envbootstrap step in your README. - Use explicit relative paths (
./config/prod.env) inenv_file:so behavior does not depend on the caller’s directory. - Always run
docker composefrom the project root, or pass-fand--project-directoryso paths resolve predictably. - In CI/CD, render the env file from secrets before invoking Compose, and validate with
docker compose configfirst. - Mark genuinely optional env files
required: falseso local-only overrides do not break shared deploys.
Related Errors
no such file or directoryon-f compose.yaml— the Compose file itself is missing, not the env file.The DB_PASS variable is not set. Defaulting to a blank string— a warning that a variable was unset, not a missing file.services.web.env_file must be a string or list— a malformedenv_file:value rather than a missing file.error while interpolating ...— bad${VAR}syntax in the Compose file, separate from env-file loading.
Frequently Asked Questions
What is the difference between the .env file and env_file:? The top-level .env supplies variables for substitution inside the Compose file. env_file: injects variables into a specific container’s environment. They are loaded independently.
Why does Compose fail instead of ignoring a missing env file? By design, so you never start a container with missing configuration silently. Provide the file, fix the path, or mark it required: false.
How are env_file paths resolved? Relative to the directory containing the Compose file, not your current shell directory. Run Compose from the project root or use explicit ./ paths to avoid surprises.
Can I make an env file optional? Yes, in Compose v2.24+ use the long form with required: false so a missing file is skipped instead of erroring. Generate Compose and env-file fixes fast with the DevOps AI prompt library.
How do I handle .env in CI where it is git-ignored? Write the file from CI secrets before running Compose, then validate with docker compose config. See more Docker guides.
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.