How can I use environment variables in docker-compose?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Environment variables are a fundamental aspect of working with Docker and Docker Compose. They provide a way to make your dockerized applications more flexible and portable by allowing you to parameterize configuration values without hardcoding them in your Dockerfiles or docker-compose YAML files.
In this article, we'll explore various methods and best practices for using environment variables in Docker Compose, along with examples and explanations to help you effectively manage environment settings in your applications.
Overview of Environment Variables in Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you define the services that make up your application in a docker-compose.yml file, so they can be run together in an isolated environment. Environment variables in Docker Compose allow you to pass configuration data to your containers at runtime, making your applications more dynamic and easier to manage.
Different Ways to Use Environment Variables in Docker Compose
- Using the
environmentKey: This method involves directly specifying environment variables within the Docker Compose file. Each service can have anenvironmentsection that sets one or more variables.
- Using an
.envFile: Docker Compose supports loading environment variables from a.envfile in the same directory as yourdocker-compose.ymlfile. Variables defined in the.envfile can be referenced in your Docker Compose file. This method helps keep sensitive data out of version control.
Example .env file:
Example docker-compose.yml file:
- Using Variable Substitution in the Compose File: This involves referencing environment variables directly inside the Compose file using
$VARIABLE_NAMEor ${VARIABLE_NAME}syntax. If not set, you can also specify a default value.
- Using Docker CLI to Set Environment Variables: You can pass environment variables at runtime using the
--env-fileor-eflag withdocker-compose runordocker-compose up.
Using --env-file:
Using -e:
Best Practices for Managing Environment Variables
- Avoid Hardcoding configuration values that are likely to change across environments (development, testing, production) in your Compose files.
- Use
.envFiles to keep your configurations organized and easy to manage. They can be ignored in.gitignoreto avoid exposing sensitive data in version control. - Use Variables for Ports and Secrets: Parameterize sensitive data like API keys, database passwords, and hostnames.
- Consistent Naming: Use consistent and meaningful names for your environment variables to maintain clarity and organization.
Table of Common Environment Variable Practices
| Method | Use Case | Pros | Cons |
environment Key | Direct declaration within docker-compose.yml | Easy to see and manage directly in YAML | Hardcoded, less secure |
.env File | Manage environment variables in a separate file | Keeps secrets out of YAML and version control | Requires careful management of the file |
| Variable Substitution | Dynamic configuration with defaults | Flexible with defaults | Complexity with syntax and defaults |
Docker CLI (--env-file or -e) | Run-time flexibility for overriding defaults | Powerful for temporary changes | Not ideal for long-term configuration |
Additional Considerations
- Security: While environment variables provide a convenient way to configure your applications, be aware that they aren't inherently secure. Ensure that your
.envfiles are kept secure and consider using Docker secrets for sensitive data in production environments. - Complex Configurations: For more complex environment management, consider tools like HashiCorp Vault or AWS Secrets Manager to dynamically manage configuration values and secrets.
- Testing Across Environments: Using environment variables, you can easily switch configurations to test your applications in different environments without altering the application code.
In conclusion, environment variables in Docker Compose offer a flexible and effective way to manage configuration settings. By using these techniques and best practices, you can ensure that your applications are portable, secure, and easy to configure across different environments.

