Mastering Docker Environment Variable Precedence
What are env variables?
Environment variables are a fundamental concept in containerized applications that allow you to configure and customize applications running inside containers. They are key-value pairs containing information that applications can use to adjust their behaviour at runtime. These variables are injected into the container’s environment and can be used to pass configuration data, secrets, or any other dynamic information required by the application.
Key benefits:
- Flexibility Across Environments: Docker environment variables enable environment-specific (development, testing, production, etc.) application configuration without altering code.
- Dynamic Configuration: Use environment variables for dynamic configuration during the build process, passing values with the ARG command in the Dockerfile and making them accessible during both build and runtime with the ENV command.
- Scalability and Maintainability: Manage scalability with proper use of environment variables and understanding their precedence for a scalable architecture.
- Consistent Deployment Pipeline: In a CI/CD pipeline, mastering Docker environment variable precedence ensures consistent configurations across stages, minimizing production issues from testing or development discrepancies.
- Security: Enhance security by avoiding direct storage of sensitive information in the Dockerfile.
- Isolation of Application Configuration Data: Isolate application configuration data with environment variables for focused developer access.
- Avoiding Hardcoding: Avoid code hardcoding by using Docker environment variables for flexible configuration updates.
- Debugging and Troubleshooting: Effective configuration management is crucial for debugging and troubleshooting, preventing runtime errors and unexpected behaviour.
- Adaptability to Changing Requirements: Adapt to changing requirements easily with Docker environment variables, avoiding significant code changes.
- Collaboration and Teamwork: Standardized Docker environment variables foster seamless collaboration in team environments, reducing misconfigurations and boosting productivity.
Variable precedence:
In Docker, environment variables play a crucial role in configuring and customizing the behaviour of applications running inside containers. When you set the same environment variable in multiple sources, Docker Compose uses a precedence rule to resolve the value for the variable. The order of precedence (highest to lowest) is as follows:
- Set using
docker compose run -e
in the CLI
Override Dockerfile-defined environment variables with docker run -e
option. For instance,
docker run -e NODE_ENV=development my_image
sets NODE_ENV to development, overriding Dockerfile values. Directly set variables with docker-compose run -e
, e.g.,
docker-compose run -e NODE_ENV=custom my_service
overrides lower-level values.
2. Substituted from your shell
Utilize shell environment variables in Docker Compose files. Setting an environment variable in the shell where Docker Compose is executed overrides lower-level values. For instance, running
export NODE_ENV=test
before docker-compose up
sets NODE_ENV to test, irrespective of Dockerfile, docker run, or docker-compose.yml settings.
3. Set using the environment
attribute in the Compose file
In a docker-compose.yml file, use the environment
key to specify service-specific variables, overriding lower-level values. For example:
services:
my_service:
image: my_image
environment:
NODE_ENV: staging
This starts my_service
with NODE_ENV set to staging, overriding Dockerfile or docker run settings.
4. Use of the --env-file
argument in the CLI
docker compose --env-file ./config/.env.dev up
5. Use of the env_file
attribute in the Compose file
web:
env_file:
- web-variables.env
6. Set using an .env
file placed at the base of your project directory
cat .env
TAG=v1.5
cat compose.yml
services:
web:
image: "webapp:${TAG}"
7. Set in a container image in the ENV directive. Having any ARG
or ENV
setting in a Dockerfile
evaluates only if there is no Docker Compose entry for environment
, env_file
or run --env
. You can set default environment variables in your Dockerfile with the ENV instruction. For example, you might have a line in your Dockerfile like ENV NODE_ENV=production
. This sets a default value that will be used if no other value is provided at a higher level.
ENV NODE_ENV=staging
Best practices:
1. Use the -e
flag or.env
file.
2. Keep your .env
file secure: The .env file can contain sensitive information, such as API keys and database passwords. It’s crucial to keep your .env
file safe and not commit it to your code repository.
3. Use descriptive variable names.
4. Define default values.
5. Update your .env file regularly.
6. Utilize Docker Compose: When working with Docker Compose, environment variables can be defined in the docker-compose.yml
file, providing a centralized location for configuration.
In conclusion, understanding Docker environment variables is crucial for a robust setup. Master variable precedence to enhance flexibility, scalability, and maintainability. Apply this knowledge by experimenting with configurations and implementing best practices.
Share your thoughts in the comments, and stay tuned for more insights into our ongoing journey of mastering new concepts together.