Environment variables are a powerful way to configure your applications and services without hardcoding values into your codebase. Docker Compose allows you to define and manage environment variables in a flexible and efficient manner. In this section, we will cover the following topics:
- Introduction to Environment Variables
- Defining Environment Variables in Docker Compose
- Using
.env
Files - Overriding Environment Variables
- Practical Examples
- Exercises
- Introduction to Environment Variables
Environment variables are key-value pairs that can influence the behavior of running processes. They are commonly used to:
- Configure application settings (e.g., database URLs, API keys).
- Control the behavior of services (e.g., debug mode, logging levels).
- Pass sensitive information securely.
- Defining Environment Variables in Docker Compose
In Docker Compose, you can define environment variables in several ways:
a. Using the environment
Key
You can specify environment variables directly within the docker-compose.yml
file using the environment
key.
version: '3.8' services: web: image: my-web-app:latest environment: - DEBUG=true - DATABASE_URL=postgres://user:password@db:5432/mydatabase db: image: postgres:latest environment: - POSTGRES_USER=user - POSTGRES_PASSWORD=password - POSTGRES_DB=mydatabase
b. Using the env_file
Key
You can also specify environment variables in an external file and reference it in your docker-compose.yml
file using the env_file
key.
version: '3.8' services: web: image: my-web-app:latest env_file: - .env db: image: postgres:latest env_file: - .env
- Using
.env
Files
.env
FilesThe .env
file is a simple text file containing key-value pairs of environment variables. It is typically placed in the root directory of your project.
Example .env
file:
DEBUG=true DATABASE_URL=postgres://user:password@db:5432/mydatabase POSTGRES_USER=user POSTGRES_PASSWORD=password POSTGRES_DB=mydatabase
- Overriding Environment Variables
Docker Compose allows you to override environment variables defined in the docker-compose.yml
file or .env
file by specifying them in the shell environment when running docker-compose
commands.
In this example, the DEBUG
and DATABASE_URL
environment variables will override the values defined in the docker-compose.yml
file or .env
file.
- Practical Examples
Example 1: Using Environment Variables for Configuration
version: '3.8' services: web: image: my-web-app:latest environment: - DEBUG=${DEBUG} - DATABASE_URL=${DATABASE_URL} db: image: postgres:latest environment: - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_DB=${POSTGRES_DB}
Example 2: Using an .env
File
Create a .env
file with the following content:
DEBUG=true DATABASE_URL=postgres://user:password@db:5432/mydatabase POSTGRES_USER=user POSTGRES_PASSWORD=password POSTGRES_DB=mydatabase
Reference the .env
file in your docker-compose.yml
:
version: '3.8' services: web: image: my-web-app:latest env_file: - .env db: image: postgres:latest env_file: - .env
- Exercises
Exercise 1: Define Environment Variables
- Create a
docker-compose.yml
file for a simple web application and a database. - Define environment variables for the database connection string and debug mode directly in the
docker-compose.yml
file.
Solution:
version: '3.8' services: web: image: my-web-app:latest environment: - DEBUG=true - DATABASE_URL=postgres://user:password@db:5432/mydatabase db: image: postgres:latest environment: - POSTGRES_USER=user - POSTGRES_PASSWORD=password - POSTGRES_DB=mydatabase
Exercise 2: Use an .env
File
- Create an
.env
file with the necessary environment variables. - Modify the
docker-compose.yml
file to use the.env
file.
Solution:
Create a .env
file:
DEBUG=true DATABASE_URL=postgres://user:password@db:5432/mydatabase POSTGRES_USER=user POSTGRES_PASSWORD=password POSTGRES_DB=mydatabase
Modify the docker-compose.yml
file:
version: '3.8' services: web: image: my-web-app:latest env_file: - .env db: image: postgres:latest env_file: - .env
Conclusion
In this section, we explored how to use environment variables in Docker Compose to configure and manage your services. We covered defining environment variables directly in the docker-compose.yml
file, using .env
files, and overriding environment variables. By mastering these techniques, you can create more flexible and maintainable Docker Compose configurations.
Docker: From Beginner to Advanced
Module 1: Introduction to Docker
- What is Docker?
- Installing Docker
- Docker Architecture
- Basic Docker Commands
- Understanding Docker Images
- Creating Your First Docker Container
Module 2: Working with Docker Images
- Docker Hub and Repositories
- Building Docker Images
- Dockerfile Basics
- Managing Docker Images
- Tagging and Pushing Images
Module 3: Docker Containers
- Running Containers
- Container Lifecycle
- Managing Containers
- Networking in Docker
- Data Persistence with Volumes
Module 4: Docker Compose
- Introduction to Docker Compose
- Defining Services in Docker Compose
- Docker Compose Commands
- Multi-Container Applications
- Environment Variables in Docker Compose
Module 5: Advanced Docker Concepts
- Docker Networking Deep Dive
- Docker Storage Options
- Docker Security Best Practices
- Optimizing Docker Images
- Docker Logging and Monitoring
Module 6: Docker in Production
- CI/CD with Docker
- Orchestrating Containers with Docker Swarm
- Introduction to Kubernetes
- Deploying Docker Containers in Kubernetes
- Scaling and Load Balancing