Using Environment Variables in Compose

Tutorial 4 of 5

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

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

  2. Create a Docker Compose file that uses an environment file to set the root password for a MySQL service.

Solutions

  1. Here is how you can set the MYSQL_ROOT_PASSWORD environment variable:
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: mysecretpassword
  1. 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.