This guide explains how environment variables are loaded in different deployment scenarios.
The environment variables are injected by the container runtime or shell environment. This approach ensures compatibility with:
- ✅ Docker Compose - via
env_filedirective - ✅ Kubernetes - via ConfigMaps and Secrets
- ✅ Local Development - via shell export or IDE configuration
The docker-compose.yml file uses the env_file directive to load environment variables from ./api/.env:
api:
env_file:
- ./api/.env
environment:
- DATABASE_URL=postgresql://...Important: The env_file directive reads the .env file from the host and injects variables as environment variables into the container. It does not copy the file into the container.
-
Create or update
api/.envwith your configuration:cp api/.env.example.full api/.env # Edit api/.env with your settings -
Start services:
docker-compose up --build -d
-
Verify environment variables are loaded:
docker-compose exec api env | grep DATABASE_URL
- Check that
env_fileis specified indocker-compose.yml - Verify the
.envfile exists at./api/.env - Restart services:
docker-compose restart - Check logs:
docker-compose logs api
- Never commit
.envfiles - they contain secrets - Use
.env.exampleto document required variables - In K8s, use Secrets for sensitive data
- In Docker Compose, use
env_filefor convenience - For CI/CD, inject env vars via pipeline configuration