6.1 Basics of Environment Variables
Environment variables play a super important role in app configurations, letting you dynamically tweak the behavior and settings of your services without messing around with the app code. Docker Compose supports using environment variables for setting up containers, giving you flexibility and making configuration management way easier.
One of the handiest ways to handle environment variables is by using .env files. In this lecture, we're gonna dive into how to use environment variables in Docker Compose, including creating and working with .env files.
Basics of Environment Variables in Docker Compose
Environment variables can be used in the compose.yaml file to parameterize your configs. These variables might come from an .env file, directly inside the compose.yaml, or passed in through the command line.
Example of using environment variables in compose.yaml:
In this example, we're using the variables NGINX_VERSION and HOST_PORT, where their values can be set in an .env file or specified via the command line.
version: '3.8'
services:
web:
image: nginx:${NGINX_VERSION}
ports:
- "${HOST_PORT}:80"
6.2 Using .env Files
1. Creating a .env File
The .env file is placed in the same directory as compose.yaml and contains "key-value" pairs that define the values of environment variables. Each variable is specified on a new line.
Example of a .env File:
NGINX_VERSION=latest
HOST_PORT=8080
DB_USER=admin
DB_PASSWORD=secret
2. Integrating the .env File with Docker Compose
Docker Compose automatically loads variables from the .env file if it is located in the same directory as compose.yaml.
Example of a compose.yaml File Using Environment Variables:
In this example, the variables NGINX_VERSION, HOST_PORT, DB_USER, and DB_PASSWORD are taken from the .env file.
version: '3.8'
services:
web:
image: nginx:${NGINX_VERSION}
ports:
- "${HOST_PORT}:80"
db:
image: postgres:latest
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
6.3 Passing Variables
Passing variables via the command line
You can pass environment variables directly through the command line using the export command on Unix-like systems or by using the --env-file parameter in Docker Compose.
Example of using export:
export NGINX_VERSION=latest
export HOST_PORT=8080
docker compose up
Example of using --env-file:
You can specify an alternative environment variables file using the --env-file parameter.
docker compose --env-file .env.dev up
Using built-in environment variables in Docker Compose
Docker Compose supports built-in environment variables like ${PWD}, representing the current working directory.
Example:
version: '3.8'
services:
app:
image: myapp:latest
volumes:
- ${PWD}/app:/app
6.4 Practical Examples
Some practical examples of using environment variables:
Example 1: Setting up a web server and a database
Create a file .env with parameters for the web server and database:
NGINX_VERSION=1.19.3
HOST_PORT=8080
DB_USER=myuser
DB_PASSWORD=mypassword
Create a file compose.yaml that uses these variables:
version: '3.8'
services:
web:
image: nginx:${NGINX_VERSION}
ports:
- "${HOST_PORT}:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
db:
image: postgres:latest
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
Run Docker Compose:
docker compose up
Example 2: Separating development and production environments
Create two .env files for different environments: .env.dev and .env.prod.
File .env.dev:
NGINX_VERSION=latest
HOST_PORT=8080
DB_USER=devuser
DB_PASSWORD=devpassword
File .env.prod:
NGINX_VERSION=1.19.3
HOST_PORT=80
DB_USER=produser
DB_PASSWORD=prodpassword
Create a compose.yaml file that uses variables from these files:
version: '3.8'
services:
web:
image: nginx:${NGINX_VERSION}
ports:
- "${HOST_PORT}:80"
db:
image: postgres:latest
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
Run Docker Compose for development:
docker compose --env-file .env.dev up
Run Docker Compose for production:
docker compose --env-file .env.prod up
GO TO FULL VERSION