docker-compose
environment variables
docker
devops
containerization

How can I use environment variables in docker-compose?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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

  1. Using the environment Key: This method involves directly specifying environment variables within the Docker Compose file. Each service can have an environment section that sets one or more variables.
yaml
1version: '3.8'
2services:
3  web:
4    image: 'nginx:alpine'
5    environment:
6      - NGINX_HOST=localhost
7      - NGINX_PORT=80
  1. Using an .env File: Docker Compose supports loading environment variables from a .env file in the same directory as your docker-compose.yml file. Variables defined in the .env file can be referenced in your Docker Compose file. This method helps keep sensitive data out of version control.

Example .env file:

plaintext
NGINX_HOST=localhost
NGINX_PORT=8080

Example docker-compose.yml file:

yaml
1version: '3.8'
2services:
3  web:
4    image: 'nginx:alpine'
5    ports:
6      - "${NGINX_PORT}:80"
  1. Using Variable Substitution in the Compose File: This involves referencing environment variables directly inside the Compose file using $VARIABLE_NAME or ${VARIABLE_NAME} syntax. If not set, you can also specify a default value.
yaml
1version: '3.8'
2services:
3  database:
4    image: 'postgres:alpine'
5    environment:
6      POSTGRES_DB: ${DB_NAME:-default_db}
7      POSTGRES_USER: ${DB_USER:-default_user}
8      POSTGRES_PASSWORD: ${DB_PASSWORD:-default_password}
  1. Using Docker CLI to Set Environment Variables: You can pass environment variables at runtime using the --env-file or -e flag with docker-compose run or docker-compose up.

Using --env-file:

bash
docker-compose --env-file ./myenv up

Using -e:

bash
DB_USER=myuser DB_PASSWORD=mypassword docker-compose up

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 .env Files to keep your configurations organized and easy to manage. They can be ignored in .gitignore to 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

MethodUse CaseProsCons
environment KeyDirect declaration within docker-compose.ymlEasy to see and manage directly in YAMLHardcoded, less secure
.env FileManage environment variables in a separate fileKeeps secrets out of YAML and version controlRequires careful management of the file
Variable SubstitutionDynamic configuration with defaultsFlexible with defaultsComplexity with syntax and defaults
Docker CLI (--env-file or -e)Run-time flexibility for overriding defaultsPowerful for temporary changesNot 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 .env files 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.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.