How do I use environment variables in .env file in docker-compose.yml?

2 min read 06-10-2024
How do I use environment variables in .env file in docker-compose.yml?


Using Environment Variables in Docker Compose with .env Files

Docker Compose provides a powerful way to manage multi-container applications. One of its key features is the ability to use environment variables, which can be defined in a separate .env file and injected into your containers. This allows for flexible configuration and avoids hardcoding sensitive information directly into your Docker Compose files.

The Problem: Managing Configuration in Docker Compose

Imagine you're building a web application with a database backend. You need to configure your web server (e.g., Nginx) and database (e.g., MySQL) with the appropriate settings for your specific environment (development, testing, production). Hardcoding these settings directly in your docker-compose.yml file would lead to repetitive updates and potential security risks.

Solution: Using Environment Variables and .env Files

The solution is to use environment variables and a .env file. Here's how it works:

1. Create a .env file:

DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_NAME=mydatabase
DATABASE_USER=myuser
DATABASE_PASSWORD=mypassword

2. Modify your docker-compose.yml:

version: "3.7"

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    environment:
      - DATABASE_HOST=${DATABASE_HOST}
      - DATABASE_PORT=${DATABASE_PORT}
      - DATABASE_NAME=${DATABASE_NAME}
      - DATABASE_USER=${DATABASE_USER}
      - DATABASE_PASSWORD=${DATABASE_PASSWORD}
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=${DATABASE_PASSWORD}
      - MYSQL_DATABASE=${DATABASE_NAME}
      - MYSQL_USER=${DATABASE_USER}

Explanation:

  • .env file: This file contains your environment variables as key-value pairs.
  • docker-compose.yml: You reference these variables using the syntax $ENV_VARIABLE within the environment section of your services.
  • Environment Injection: When Docker Compose starts your containers, it reads the .env file and injects the variables as environment variables into the containers.

Advantages of this Approach:

  • Configuration Flexibility: Easily switch between different configurations (development, testing, production) by modifying the .env file.
  • Security: Keep sensitive data like passwords out of your Docker Compose files and potentially out of version control.
  • Code Reusability: You can easily reuse the same Docker Compose file across multiple environments.

Additional Considerations:

  • Environment Variables Precedence: The environment variables defined in your .env file can be overridden by the variables set in your docker-compose.yml file, and those can be further overridden by environment variables set on the command line.
  • Security Best Practices: It's crucial to use a .gitignore file to exclude your .env file from version control to prevent accidentally committing sensitive data.
  • Other Environment Variables: You can also leverage other environment variables, including:
    • DOCKER_HOST: The hostname of your Docker host
    • DOCKER_PORT: The port that Docker is listening on
    • DOCKER_CERT_PATH: The path to your Docker certificates

Conclusion:

Using environment variables and .env files in Docker Compose is an effective approach to managing configuration and keeping your applications secure. It promotes flexibility, simplifies development workflows, and ensures a consistent environment for your applications.