Docker / Docker Compose
Using Environment Variables in Compose
In this tutorial, you'll learn how to use environment variables in Docker Compose to manage configuration settings for your services. We'll also cover security best practices for …
Section overview
5 resourcesCovers how to manage multi-container applications using Docker Compose.
Using Environment Variables in Compose
1. Introduction
In this tutorial, we'll explore how to use environment variables in Docker Compose to manage the configuration settings for your services. By the end of this tutorial, you should be able to:
- Understand what environment variables are and how Docker Compose uses them
- Use environment variables in Docker Compose files
- Securely handle sensitive data with environment variables
Prerequisites: Basic knowledge of Docker and Docker Compose is required.
2. Step-by-Step Guide
Environment variables are a way of passing configuration settings to your applications. Docker Compose uses them to set environment variables inside containers, or to define values that are used in the Compose file.
Docker Compose supports two ways to set environment variables:
- Defining environment variables in the service
- Using an environment file
Defining environment variables in the service
You can set environment variables directly in a service’s configuration:
services:
web:
environment:
- VARIABLE_NAME=value
Using an environment file
Alternatively, you can use a separate file to specify environment variables:
services:
web:
env_file:
- .env
In the .env file, you should define your variables like so:
VARIABLE_NAME=value
Security Best Practices
Sensitive data, such as passwords or API keys, should not be included directly in the Docker Compose file. Instead, use environment variables to pass this data to the containers.
3. Code Examples
Example 1: Setting a simple environment variable
In the following Docker Compose file, we set an environment variable DEBUG for the web service:
services:
web:
environment:
- DEBUG=true
Example 2: Using an environment file
In this example, we’ll use an environment file to set the DEBUG variable:
services:
web:
env_file:
- .env
And in the .env file:
DEBUG=true
4. Summary
In this tutorial, we've learned how to use environment variables in Docker Compose to manage configuration settings for your services. We've also covered how to securely handle sensitive data with environment variables.
To learn more, you can refer to the official Docker Compose documentation on environment variables.
5. Practice Exercises
-
Create a Docker Compose file that defines an environment variable for a MySQL service setting the root password. Hint: The environment variable is MYSQL_ROOT_PASSWORD.
-
Create a Docker Compose file that uses an environment file to set the root password for a MySQL service.
Solutions
- Here is how you can set the MYSQL_ROOT_PASSWORD environment variable:
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysecretpassword
- To use an environment file, you would do the following:
services:
db:
image: mysql:5.7
env_file:
- .env
And in the .env file:
MYSQL_ROOT_PASSWORD=mysecretpassword
Remember, never commit sensitive data, such as your database password, to your version control system. Always use environment variables for this kind of data.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article