How to Use Environment Variables in Docker Compose

Avatar

By squashlabs, Last Updated: October 21, 2023

How to Use Environment Variables in Docker Compose

Environment variables in Docker Compose allow you to define and manage the configuration of your Docker containers. They provide a flexible way to customize container behavior without modifying the container image itself. In this guide, we will explore how to use environment variables in Docker Compose, including defining variables, referencing them in your Compose file, and setting default values.

Defining Environment Variables

To define environment variables in Docker Compose, you can use the environment key within the services section of your Compose file. Each variable is defined as a key-value pair, where the key represents the variable name and the value represents its initial value.

Here is an example of how to define environment variables in a Compose file:

version: '3.9'
services:
  web:
    image: nginx
    environment:
      - ENV_VAR1=value1
      - ENV_VAR2=value2

In this example, we have defined two environment variables, ENV_VAR1 and ENV_VAR2, with their respective values value1 and value2.

Related Article: Build a Chat Web App with Flask, MongoDB, Reactjs & Docker

Referencing Environment Variables

Once you have defined environment variables in your Docker Compose file, you can reference them within the configuration of your services using the ${VARIABLE} syntax. This allows you to dynamically insert the values of environment variables into your container configuration.

Here is an example of how to reference environment variables in a Compose file:

version: '3.9'
services:
  web:
    image: nginx
    environment:
      - ENV_VAR1=${ENV_VAR1}
      - ENV_VAR2=${ENV_VAR2}

In this example, the values of ENV_VAR1 and ENV_VAR2 defined in the environment section will be used as the values for the respective environment variables within the container.

Default Values

You can also provide default values for environment variables in Docker Compose. This allows you to specify a fallback value in case the variable is not set externally. To define a default value, you can use the ${VARIABLE:-default} syntax.

Here is an example of how to define default values for environment variables in a Compose file:

version: '3.9'
services:
  web:
    image: nginx
    environment:
      - ENV_VAR1=${ENV_VAR1:-default_value1}
      - ENV_VAR2=${ENV_VAR2:-default_value2}

In this example, if the environment variables ENV_VAR1 and ENV_VAR2 are not set externally, their values will default to default_value1 and default_value2, respectively.

Best Practices for Using Environment Variables in Docker Compose

When working with environment variables in Docker Compose, consider the following best practices:

1. Use descriptive variable names: Choose meaningful names for your environment variables to improve readability and maintainability of your Compose file.

2. Store sensitive information securely: Avoid storing sensitive data, such as passwords or API keys, directly in your Compose file. Instead, consider using a secrets management solution or external configuration files.

3. Use separate .env files for local development: To simplify local development, you can use separate .env files to define environment variables specific to your local environment. Docker Compose automatically loads variables from a .env file located in the same directory as your Compose file.

4. Consider using an orchestration tool: If your application requires more complex management of environment variables or you have multiple services to coordinate, you might consider using an orchestration tool like Kubernetes or Docker Swarm.

Related Article: Build a Movie Search App with GraphQL, Node & TypeScript

You May Also Like

Tutorial: Building a Laravel 9 Real Estate Listing App

Step 1: Building a Laravel 9 Real Estate Listing App will become a breeze with this step-by-step tutorial. Learn how to create a powerful single-app using Laravel 9,... read more

Tutorial: Managing Docker Secrets

Managing secrets in Docker is essential for maintaining security in your applications. This tutorial provides a comprehensive guide to managing Docker secrets. From... read more

How to Run a Docker Instance from a Dockerfile

Running a Docker instance from a Dockerfile is a fundamental skill for software engineers. This article provides a step-by-step guide on creating a Dockerfile, building... read more

How to Copy Files From Host to Docker Container

Transferring files from a host to a Docker container can be a simple task with the docker cp command. This article provides a step-by-step guide on how to use this... read more

How to Pass Environment Variables to Docker Containers

Passing environment variables to Docker containers is a crucial aspect of containerization. This article provides a practical guide on how to achieve this using... read more