Docker Compose .env Generator
docker-compose auto-loads a .env file from the same directory as the compose file. Variables can be referenced in compose.yml as ${VAR}. This generator produces a compose-idiomatic .env.
COMPOSE_PROJECT_NAME=myapp POSTGRES_USER=app POSTGRES_PASSWORD=83006bcbbfa1472935a86426998832746ab268a5b30e985e POSTGRES_DB=app POSTGRES_PORT=5432 REDIS_PORT=6379 APP_PORT=3000 NODE_ENV=production
Secrets regenerate on every page load. Want to combine stacks? Use the full generator.
How to use this .env in Docker Compose
- 1.Save as .env next to your docker-compose.yml.
- 2.Reference in compose.yml with ${POSTGRES_PASSWORD} syntax.
- 3.Run docker compose config to preview the resolved YAML before starting containers.
- 4.In prod, use docker secrets or your host's env injection instead of committing a compose .env.
Gotchas
- ⚠The compose .env is different from the .env passed via env_file: — the former is for substitution, the latter is injected into the container.
- ⚠Multi-line values break compose substitution. Keep each value on one line.
- ⚠${VAR:-default} gives a fallback; ${VAR:?err} fails the build if unset. Use the error form for required vars.
Common keys explained
COMPOSE_PROJECT_NAMEPrefix for container names. Avoids collisions across projects.
POSTGRES_USERPassed to the official postgres image. Bootstraps the DB user.
POSTGRES_PASSWORDSet to a generated value — weak passwords get brute-forced.
POSTGRES_DBDefault database created on first boot.
APP_PORTHost port for the app service. Referenced as ${APP_PORT}:3000 in compose.
Loading and validating the .env in Docker Compose
services:
app:
build: .
env_file: .env # injected into the container at runtime
environment:
NODE_ENV: production # wins over the same key in env_file
DATABASE_URL: postgres://app:${POSTGRES_PASSWORD}@db:5432/app
depends_on: [db]
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
# ${VAR} is substituted by Compose from the project .env;
# :? makes "docker compose up" fail if it is missing.Docker Compose: env vars in development, staging and production
Compose uses .env in two different ways. The .env next to compose.yaml fills ${VAR} placeholders inside the YAML. env_file: passes a file's variables into the container. They are often the same file, but they are separate mechanisms, and that confusion causes most Docker env bugs.
Never put secrets in a Dockerfile ENV or ARG line: both are stored in the image layers and visible with docker history. Pass secrets at runtime (env_file, orchestrator secrets) or use BuildKit secret mounts (RUN --mount=type=secret) when a build step needs one.
Docker Compose: .env errors and fixes
WARN: The "POSTGRES_PASSWORD" variable is not set. Defaulting to a blank string.Compose found no value for ${POSTGRES_PASSWORD}. Put it in the .env next to compose.yaml, or export it in the shell. env_file: does not feed ${} substitution.
Container sees old values after editing .envEnvironment is fixed when the container is created. Run docker compose up -d again, which recreates changed services. A restart alone keeps the old env.
Value contains literal quotes inside the containerOlder Compose versions passed quotes through in env_file. Compose v2 strips them. Check docker compose version, or remove the quotes.
Docker Compose .env FAQ
What is the difference between env_file and environment in Compose?
env_file loads variables from a file. environment sets them inline. If both define the same key, environment wins.
Should .env be copied into the Docker image?
No. Add .env to .dockerignore so COPY . . cannot bake secrets into a layer, and inject values at runtime.
How do I see the env inside a running container?
docker compose exec app env, or docker inspect <container> for the configured values. Remember that docker inspect also shows secrets to anyone with Docker access.