Environment variables are important for managing and configuring Dockerized applications. They allow you to externalize settings like database credentials, API keys, and environment modes. This means you don’t need to hardcode them into your containers. Using environment variables helps you switch easily between development, testing, and production environments. It also keeps sensitive data secure.
In this article, we’ll cover several ways to define and use environment variables in Docker Compose.
Table of Contents
There are several methods to set environment variables in Docker Compose, each suited to different scenarios. Let’s explore these methods in detail.
Method 1: Inline Environment Variables
You can define environment variables directly in the docker-compose.yml file using the environment key. This method is simple and works well for simple setups.
version: '3'
services:
web:
image: nginx
environment:
- NGINX_HOST=example.com
- NGINX_PORT=80
When you run docker-compose up, the web service will start with the specified environment variables.
Method 2: Using a .env File
For better organization and security, you can use an .env file to store environment variables. Docker Compose will automatically load this file and inject the variables into your services.
Create a .env file with the following content.
NGINX_HOST=example.com
NGINX_PORT=80
Reference these variables in your docker-compose.yml file.
version: '3'
services:
web:
image: nginx
environment:
- NGINX_HOST=${NGINX_HOST}
- NGINX_PORT=${NGINX_PORT}
Running docker-compose up will produce the same result, with environment variables pulled from the .env file.
Method 3: Using Docker Compose Overrides
Docker Compose allows you to override environment variables defined in the docker-compose.yml file using a docker-compose.override.yml file. This is particularly useful for managing different environments.
Create a docker-compose.override.yml file:
version: '3'
services:
web:
environment:
- NGINX_HOST=override.com
- NGINX_PORT=8080
Running docker-compose up will apply the overridden variables.
Method 4: Passing Variables from the Host Environment
You can pass environment variables directly from the host system to Docker Compose. This is useful when you want to keep sensitive information out of your configuration files.
First, export the environment variable in your host system.
export NGINX_HOST=securehost.com
export NGINX_PORT=443
Then, create a docker-compose.yml file.
version: '3'
services:
web:
image: nginx
environment:
- NGINX_HOST
- NGINX_PORT
Now, run the following command:
# docker-compose up
After running the docker-compose up, the variables are injected from the host environment.
Conclusion
Eenvironment variables are essential for configuring Docker Compose applications. They provide flexibility and security by separating configuration from code. Using methods like inline variables, .env files, or passing from the host system makes managing configurations easier. For sensitive data, always use .env files and avoid hardcoding values in docker-compose.yml.
FAQs
1. Can I specify an alternative .env file for Docker Compose?
Yes, you can specify an alternative .env file using the --env-file option.
2. How can I inspect the environment variables of a running container in Docker Compose?
You can inspect the environment variables of a running container using: docker compose exec service_name env
3. Can I set environment variables at runtime with Docker Compose?
Yes, you can pass environment variables at runtime using the -e flag.