Environment Variable Management in Docker: .env vs. stack.env
Overview
When deploying containerized applications, managing environment variables is crucial for configuration flexibility. Docker provides two common approaches:
-
.envFile: Used with Docker Compose for standalone deployments. -
stack.envFile: Used with Docker Swarm for orchestrated, multi-host deployments.
Understanding the differences between these files will help you choose the right method for your deployment environment.
Docker Compose and the .env File
Key Features
-
Variable Substitution:
The.envfile supports variable substitution, allowing you to define placeholders in yourdocker-compose.ymlfile.Example:
image: nginx:${VERSION}Here,
${VERSION}is replaced with the value defined in the.envfile. -
Dynamic Configuration:
You can easily update port numbers, image versions, and other configuration settings by modifying the.envfile without changing the main Compose file.
Use Cases
-
Local Development:
Simplifies the configuration process by decoupling variable values from the compose file. -
Standalone Deployments:
Docker Compose automatically detects and loads the.envfile when running commands likedocker-compose up.
Docker Swarm and the stack.env File
Key Features
-
Limited to Environment Variables:
Thestack.envfile is only used to set environment variables under theenvironmentfield in your stack configuration. It does not support variable substitution for other settings such as port numbers or image versions. -
No Variable Substitution:
Unlike the.envfile, thestack.envfile cannot dynamically replace placeholders in the configuration file.Implication:
Values like image versions and port numbers must be hard-coded, set as defaults, or managed by an external process.
Use Cases
-
Production Deployments:
Ideal for multi-host environments managed by Docker Swarm, where consistency across nodes is critical. -
Limited Dynamic Updates:
For configurations that require updating after the initial deployment, you need alternative methods such as the Portainer UI or webhooks.
Updating Environment Variables in Docker Swarm
Since stack.env does not support variable substitution, you can use webhooks or the Portainer UI for dynamic updates:
Using Webhooks
-
Initial Deployment:
Set a default value in yourdocker-compose.ymlor via the Portainer UI for the first deployment. -
Updating Values:
Use a webhook call to update the environment variable later.Example Webhook URL:
https://localhost:9471/api/stacks/webhooks/1fefe43c-9373-46bf-8cfa-3bf687a294c0?FRESHRSS_TAG=latestThis URL updates the
FRESHRSS_TAGvariable tolatest. Integrate this process into your CI/CD pipelines for seamless updates.
Additional Resources
-
Portainer Webhook Documentation: For more detailed instructions on using webhooks with Docker Swarm, refer to the official documentation.
Summary
-
.envin Docker Compose: Supports variable substitution, ideal for dynamic configurations in local or standalone environments. -
stack.envin Docker Swarm: Limited to setting environment variables without substitution; use defaults or external tools (like Portainer webhooks) for updates. -
Dynamic Updates: In Docker Swarm, update configuration values through the Portainer UI or webhooks when direct substitution is not available.
By choosing the appropriate method based on your deployment environment, you can effectively manage configuration changes and maintain consistency across your Docker applications.