10. Engine Configuration
Hardcoding configurations inside a container image violates the core principle of containerization: “Build once, run anywhere.” A single image should be deployable to Development, Staging, and Production simply by swapping its configuration at runtime.
10.1 Configuration Strategies
Section titled “10.1 Configuration Strategies”There are several ways to pass configuration into a container.
Environment Variables
Section titled “Environment Variables”The most popular and recommended approach for cloud-native applications (following the “Twelve-Factor App” methodology). You inject configuration strings directly into the container process.
- Example: Setting the logging level or defining the backend API URL.
docker run -e LOG_LEVEL=DEBUG my-app.env Files
Section titled “.env Files”Passing dozens of -e flags in the terminal is error-prone. A .env file allows you to store key-value pairs in a single text file and load them all at once.
docker run --env-file ./production.env my-appConfiguration Files
Section titled “Configuration Files”Some complex applications (like Nginx, Prometheus, or PostgreSQL) require extensive configuration files (like nginx.conf) rather than simple environment variables.
- Strategy: Use a Bind Mount or a Docker Config (in Swarm) to mount the
.conffile from the host directly into the container’s filesystem at runtime.
docker run -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginx(Note: :ro makes it read-only for security).
Runtime Configuration
Section titled “Runtime Configuration”By designing your application to read variables at runtime (when the container boots) rather than at build time (during docker build), you ensure the image remains portable across environments.
10.2 Secrets
Section titled “10.2 Secrets”A “Secret” is any sensitive data that grants access to systems, encrypts data, or authenticates users. If a secret is compromised, your entire infrastructure is at risk.
Common Secrets:
- Database passwords: Used by the application to connect to the DB.
- API keys: Used to charge a Stripe account or send emails via SendGrid.
- Tokens: JWT signing keys, OAuth client secrets.
- SSH / TLS keys: Private keys used for decrypting traffic or accessing remote servers.
10.3 Secret Management & Security Principles
Section titled “10.3 Secret Management & Security Principles”Mishandling secrets in Docker is the number one cause of container-related security breaches. You must adhere to strict rules.
Why Secrets shouldn’t be inside Dockerfiles
Section titled “Why Secrets shouldn’t be inside Dockerfiles”Never use the ENV instruction in a Dockerfile to store a secret, and never COPY a .env file containing passwords into the image.
- The Danger: Docker images are built in layers. Even if you
RUN rm secret.txtin a later step, the secret remains permanently accessible in the earlier image layer! Anyone who runsdocker historyor pulls your image from a registry can easily extract the password.
Why Secrets shouldn’t be committed to Git
Section titled “Why Secrets shouldn’t be committed to Git”If you commit your .env file to GitHub (even a private repo), it is permanently in your version history. If an employee’s laptop is compromised or the repo becomes public, the secrets are stolen instantly.
- The Solution: Always add
.envto your.gitignorefile.
Docker Secrets (Concept)
Section titled “Docker Secrets (Concept)”Docker natively supports a highly secure feature called Docker Secrets (primarily available in Docker Swarm).
- Instead of passing secrets as easily readable environment variables, Docker securely transmits the encrypted secret to the authorized container and mounts it as an in-memory
tmpfsfile (usually at/run/secrets/my_secret). - When the container dies, the file vanishes instantly from memory. The secret is never written to disk.
External Secret Managers (Concept)
Section titled “External Secret Managers (Concept)”In enterprise environments, managing .env files across hundreds of servers is impossible. Organizations use centralized Secret Managers (like AWS Secrets Manager, HashiCorp Vault, or Azure Key Vault).
- The Workflow: The container boots up with zero secrets. Its entrypoint script reaches out to the Secret Manager over the network, authenticates using an IAM role, downloads the database password directly into memory, and then launches the application. This ensures secrets are centrally rotated and never touch the developer’s laptop.