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:

  1. Introduction to Environment Variables
  2. Defining Environment Variables in Docker Compose
  3. Using .env Files
  4. Overriding Environment Variables
  5. Practical Examples
  6. Exercises

  1. 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.

  1. 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

  1. Using .env Files

The .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

  1. 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.

$ DEBUG=false DATABASE_URL=postgres://newuser:newpassword@db:5432/newdatabase docker-compose up

In this example, the DEBUG and DATABASE_URL environment variables will override the values defined in the docker-compose.yml file or .env file.

  1. 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

  1. Exercises

Exercise 1: Define Environment Variables

  1. Create a docker-compose.yml file for a simple web application and a database.
  2. 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

  1. Create an .env file with the necessary environment variables.
  2. 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.

© Copyright 2024. All rights reserved