Table of Contents
Environment variables in Docker Compose allow you to define and manage the configuration of your Docker containers. They provide a flexible way to customize container behavior without modifying the container image itself. In this guide, we will explore how to use environment variables in Docker Compose, including defining variables, referencing them in your Compose file, and setting default values.
Defining Environment Variables
To define environment variables in Docker Compose, you can use the environment
key within the services
section of your Compose file. Each variable is defined as a key-value pair, where the key represents the variable name and the value represents its initial value.
Here is an example of how to define environment variables in a Compose file:
version: '3.9' services: web: image: nginx environment: - ENV_VAR1=value1 - ENV_VAR2=value2
In this example, we have defined two environment variables, ENV_VAR1
and ENV_VAR2
, with their respective values value1
and value2
.
Related Article: How to Mount a Host Directory as a Volume in Docker Compose
Referencing Environment Variables
Once you have defined environment variables in your Docker Compose file, you can reference them within the configuration of your services using the ${VARIABLE}
syntax. This allows you to dynamically insert the values of environment variables into your container configuration.
Here is an example of how to reference environment variables in a Compose file:
version: '3.9' services: web: image: nginx environment: - ENV_VAR1=${ENV_VAR1} - ENV_VAR2=${ENV_VAR2}
In this example, the values of ENV_VAR1
and ENV_VAR2
defined in the environment section will be used as the values for the respective environment variables within the container.
Default Values
You can also provide default values for environment variables in Docker Compose. This allows you to specify a fallback value in case the variable is not set externally. To define a default value, you can use the ${VARIABLE:-default}
syntax.
Here is an example of how to define default values for environment variables in a Compose file:
version: '3.9' services: web: image: nginx environment: - ENV_VAR1=${ENV_VAR1:-default_value1} - ENV_VAR2=${ENV_VAR2:-default_value2}
In this example, if the environment variables ENV_VAR1
and ENV_VAR2
are not set externally, their values will default to default_value1
and default_value2
, respectively.
Best Practices for Using Environment Variables in Docker Compose
When working with environment variables in Docker Compose, consider the following best practices:
1. Use descriptive variable names: Choose meaningful names for your environment variables to improve readability and maintainability of your Compose file.
2. Store sensitive information securely: Avoid storing sensitive data, such as passwords or API keys, directly in your Compose file. Instead, consider using a secrets management solution or external configuration files.
3. Use separate .env files for local development: To simplify local development, you can use separate .env
files to define environment variables specific to your local environment. Docker Compose automatically loads variables from a .env
file located in the same directory as your Compose file.
4. Consider using an orchestration tool: If your application requires more complex management of environment variables or you have multiple services to coordinate, you might consider using an orchestration tool like Kubernetes or Docker Swarm.