Environment Variable in Docker Compose

Introduction

Environment variables are a powerful feature in Docker Compose that enables dynamic configuration of your applications. By using environment variables, you can easily manage sensitive information, adjust configurations for different environments, and enhance the portability of your Docker setups. This article will explain how to define, use, and manage environment variables in Docker Compose.

What Are Environment Variables?

Environment variables are key-value pairs that provide configuration information to applications at runtime. In Docker Compose, they allow you to pass settings, such as database credentials or service configurations, without hardcoding these values directly in your docker-compose.yml file.

Defining Environment Variables

There are several ways to define environment variables for use in Docker Compose:

1. Inline Definition in docker-compose.yml

You can define environment variables directly inside the docker-compose.yml file. Here’s an example:

services:
mariadb:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: "my_secure_password"

In this example:

  • The environment variable MYSQL_ROOT_PASSWORD is set directly to "my_secure_password".

2. Using the .env File

For flexibility, environment variables are often placed in a .env file that sits in the same directory as your docker-compose.yml. This keeps sensitive data out of your Compose file and allows easy configuration changes. You can reference environment variables from the .env file in your Compose file.

Example .env file:

MYSQL_ROOT_PASSWORD=my_secure_password
MYSQL_DATABASE=mydb
MYSQL_USER=myuser
MYSQL_PASSWORD=mypassword

Example docker-compose.yml file:

services:
mariadb:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}

In this example, ${MYSQL_ROOT_PASSWORD}, ${MYSQL_DATABASE}, ${MYSQL_USER}, and ${MYSQL_PASSWORD} are placeholders that Docker Compose replaces with values from the .env file.

3. System Environment Variables

If an environment variable is not defined in the .env file, Docker Compose will look for the value in the system environment. For example, if you define MYSQL_ROOT_PASSWORD in your operating system’s environment variables, Docker Compose will use that.

export MYSQL_ROOT_PASSWORD=my_system_password
docker-compose up

Docker Compose will use the system environment variable MYSQL_ROOT_PASSWORD if the .env file is not present or if the variable is not defined in the .env file.

Default Values in Environment Variables

In Docker Compose, you can assign a default value to an environment variable in case it isn’t set anywhere (neither in the .env file nor in the system environment). The syntax for specifying a default value is:

environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-defaultpassword}
  • ${MYSQL_ROOT_PASSWORD:-defaultpassword}: This means “use the value of MYSQL_ROOT_PASSWORD if it is set, otherwise fall back to defaultpassword.”

This ensures that the service has a value to work with, even if no environment variable is defined elsewhere.

Key Points About Environment Variables in Docker Compose

  1. Flexibility: They allow you to modify configurations without changing the core docker-compose.yml file.
  2. Security: By using environment variables, especially in a .env file, sensitive data such as passwords are abstracted out of the Compose file.
  3. Portability: Environment variables make it easier to move your project between different environments (e.g., development, testing, production).
  4. Default Values: Docker Compose allows default values to be specified to ensure services run even if variables aren’t defined.

Conclusion

Environment variables are a key feature in Docker Compose, providing a dynamic and flexible way to configure services. Using an .env file or system environment variables, you can manage configurations securely and efficiently. The ability to specify default values ensures your services always have the necessary configurations, even if environment variables are not explicitly set.

Related Articles:

Tri Wicaksono
Tri Wicaksono

With 15+ years in tech, I’ve worked in various engineering leadership roles including VP of Engineering, helping companies solve complex technical challenges and mentoring thousands of students.