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 …

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Covers 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

  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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random Name Generator

Generate realistic names with customizable options.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help