A complete example demonstrating how to inject environment variables into Angular applications at runtime using Docker, eliminating the need to rebuild your app for different environments.
Angular's default environment system bakes configuration values into JavaScript bundles during build time. This means:
- β Rebuilding required for different environments
- β Can't change configs after deployment
- β Makes container deployments inflexible
- β Same image can't work across multiple environments
This project demonstrates a Docker-based approach that:
- β Runtime Configuration: Inject variables when container starts
- β Zero Rebuilds: Same build works across all environments
- β Docker Native: Perfect for Kubernetes, Docker Swarm, etc.
- β Fallback Safe: Sensible defaults if variables aren't provided
- β Dev Friendly: No changes to your development workflow
- Template System: Uses
env-config.template.js
with placeholders - Startup Script: Replaces placeholders with environment variables at runtime
- Angular Service: Intelligently chooses between build-time and runtime config
- Global Config: Exposes configuration via
window.APP_CONFIG
- Node.js 18+
- Docker
- Angular CLI 19+
# Clone the repository
git clone https://github.com/timothydodd/angular-dynamic-variables.git
cd angular-dynamic-variables
# Navigate to the Angular app directory
cd angular-dynamic-variables
# Install dependencies
npm install
# Run in development mode
ng serve
# Visit http://localhost:4200
# Build the Docker image
docker build -t angular-dynamic-vars .
# Run with environment variables
docker run -p 8080:80 \
-e API_URL="https://api.production.com" \
-e FAV_COLOR="blue" \
-e FAV_CATCH_PHRASE="Hello Production!" \
angular-dynamic-vars
# Visit http://localhost:8080
βββ angular-dynamic-variables/ # Angular application directory
β βββ src/
β β βββ app/
β β β βββ _services/
β β β βββ config.service.ts # Configuration service
β β βββ environments/
β β β βββ environment.ts # Development config
β β β βββ environment.prod.ts # Production config
β β βββ env-config.js # Runtime config file
β βββ angular.json # Angular CLI configuration
β βββ package.json # Dependencies
βββ env-config.template.js # Template with placeholders
βββ generate-env-config.sh # Startup script
βββ Dockerfile # Multi-stage Docker build
The following environment variables are supported:
Variable | Description | Default |
---|---|---|
API_URL |
Backend API URL | window.location.origin |
FAV_COLOR |
Theme color | 'yellow' |
FAV_CATCH_PHRASE |
Application tagline | 'I like producing' |
- Add to template (
env-config.template.js
):
myNewVar: '${MY_NEW_VAR}',
- Update startup script (
generate-env-config.sh
):
ENV_VARS="API_URL FAV_COLOR FAV_CATCH_PHRASE MY_NEW_VAR"
- Update service (
config.service.ts
):
get myNewVar(): string {
return this.config.myNewVar;
}
- Uses
environment.ts
values - No Docker required
- Hot reload works normally
- Uses
environment.prod.ts
as fallback - Overrides with
window.APP_CONFIG
values - Requires Docker for runtime variable injection
- Development:
environment.ts
values - Production:
environment.prod.ts
fallbacks - Runtime:
window.APP_CONFIG
overrides (production only)
- Angular builds normally with
ng build --configuration production
env-config.js
is copied to build output (not processed/minified)- Docker startup script replaces template placeholders
- Generated config file overwrites the build output version
- Stage 1: Node.js container builds Angular app
- Stage 2: Nginx container serves static files + handles runtime config
version: '3.8'
services:
frontend:
build: .
ports:
- "8080:80"
environment:
- API_URL=https://api.example.com
- FAV_COLOR=green
- FAV_CATCH_PHRASE=Docker Compose Rocks!
This repository accompanies the blog post: "Dynamic Environment Variables in Angular: A Docker-Ready Solution"
Read the full tutorial here β
- Check container logs:
docker logs <container-name>
- Verify environment variables are set correctly
- Ensure
generate-env-config.sh
has execute permissions - Check
env-config.js
was generated properly in container
- Ensure Angular CLI version compatibility
- Check Node.js version (18+ required)
- Verify all dependencies are installed
This project is licensed under the MIT License
If this project helped you, please give it a β on GitHub!
Making Angular deployments more flexible, one container at a time! π’