How to Use Environment Variables in Docker Compose

Avatar

By squashlabs, Last Updated: Oct. 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: How to Mount a Host Directory as a Volume in Docker Compose

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.

You May Also Like

How To List Containers In Docker

Listing containers in Docker is a fundamental task for managing your Docker environment. This article provides a basic guide on how to list container… read more

Copying a Directory to Another Using the Docker Add Command

Copying directories in Docker using the Add command is a process that can be done in just a few steps. This article provides a step-by-step guide to … read more

How to Force Docker for a Clean Build of an Image

Building Docker images without using cache is essential for ensuring a clean build. This article provides three methods to force Docker for a clean b… 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 D… 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… read more

Quick Docker Cheat Sheet: Simplify Containerization

Simplify containerization with a concise Docker cheat sheet. Learn essential commands and tips to efficiently manage Docker containers. From getting … read more

How to Secure Docker Containers

Learn how to secure your Docker containers with practical steps to protect your applications and data. From understanding container security to imple… read more

How to Use Docker Exec for Container Commands

Running commands in a Docker container can be made easier with Docker Exec. In this tutorial, you will learn the basics of using Docker Exec and expl… read more

How to Improve Docker Container Performance

Optimize your Docker container performance with simple tips to enhance your application's speed and efficiency. Learn about Docker containers, instal… read more

How to Stop and Remove All Docker Containers

A guide on how to stop and remove all Docker containers, including step-by-step instructions for each process. Learn how to list running containers, … read more