CodeGym /Courses /Docker SELF /Environment Variables in Docker Compose

Environment Variables in Docker Compose

Docker SELF
Level 16 , Lesson 0
Available

6.1 Basics of Environment Variables

Environment variables play a super important role in app configurations, letting you dynamically tweak the behavior and settings of your services without messing around with the app code. Docker Compose supports using environment variables for setting up containers, giving you flexibility and making configuration management way easier.

One of the handiest ways to handle environment variables is by using .env files. In this lecture, we're gonna dive into how to use environment variables in Docker Compose, including creating and working with .env files.

Basics of Environment Variables in Docker Compose

Environment variables can be used in the compose.yaml file to parameterize your configs. These variables might come from an .env file, directly inside the compose.yaml, or passed in through the command line.

Example of using environment variables in compose.yaml:

In this example, we're using the variables NGINX_VERSION and HOST_PORT, where their values can be set in an .env file or specified via the command line.

Yaml

version: '3.8'

services:
  web:
    image: nginx:${NGINX_VERSION}
    ports:
      - "${HOST_PORT}:80"

6.2 Using .env Files

1. Creating a .env File

The .env file is placed in the same directory as compose.yaml and contains "key-value" pairs that define the values of environment variables. Each variable is specified on a new line.

Example of a .env File:

.env

NGINX_VERSION=latest
HOST_PORT=8080
DB_USER=admin
DB_PASSWORD=secret

2. Integrating the .env File with Docker Compose

Docker Compose automatically loads variables from the .env file if it is located in the same directory as compose.yaml.

Example of a compose.yaml File Using Environment Variables:

In this example, the variables NGINX_VERSION, HOST_PORT, DB_USER, and DB_PASSWORD are taken from the .env file.

Yaml

version: '3.8'

services:
  web:
    image: nginx:${NGINX_VERSION}
    ports:
      - "${HOST_PORT}:80"
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}

6.3 Passing Variables

Passing variables via the command line

You can pass environment variables directly through the command line using the export command on Unix-like systems or by using the --env-file parameter in Docker Compose.

Example of using export:

Terminal

export NGINX_VERSION=latest
export HOST_PORT=8080
docker compose up

Example of using --env-file:

You can specify an alternative environment variables file using the --env-file parameter.

Terminal

docker compose --env-file .env.dev up

Using built-in environment variables in Docker Compose

Docker Compose supports built-in environment variables like ${PWD}, representing the current working directory.

Example:

Yaml

version: '3.8'

services:
  app:
    image: myapp:latest
    volumes:
      - ${PWD}/app:/app

6.4 Practical Examples

Some practical examples of using environment variables:

Example 1: Setting up a web server and a database

Create a file .env with parameters for the web server and database:

.env

NGINX_VERSION=1.19.3
HOST_PORT=8080
DB_USER=myuser
DB_PASSWORD=mypassword

Create a file compose.yaml that uses these variables:

Yaml

version: '3.8'

services:
  web:
    image: nginx:${NGINX_VERSION}
    ports:
      - "${HOST_PORT}:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

Run Docker Compose:

Terminal

docker compose up

Example 2: Separating development and production environments

Create two .env files for different environments: .env.dev and .env.prod.

File .env.dev:

.env

NGINX_VERSION=latest
HOST_PORT=8080
DB_USER=devuser
DB_PASSWORD=devpassword

File .env.prod:

.env

NGINX_VERSION=1.19.3
HOST_PORT=80
DB_USER=produser
DB_PASSWORD=prodpassword

Create a compose.yaml file that uses variables from these files:

Yaml

version: '3.8'

services:
  web:
    image: nginx:${NGINX_VERSION}
    ports:
      - "${HOST_PORT}:80"
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

Run Docker Compose for development:

Terminal

docker compose --env-file .env.dev up

Run Docker Compose for production:

Terminal

docker compose --env-file .env.prod up
3
Task
Docker SELF, level 16, lesson 0
Locked
Using variables in yml
Using variables in yml
3
Task
Docker SELF, level 16, lesson 0
Locked
Passing Variables via Command
Passing Variables via Command
3
Task
Docker SELF, level 16, lesson 0
Locked
Using Multiple Environments
Using Multiple Environments
3
Task
Docker SELF, level 16, lesson 0
Locked
Setting up a database with variables
Setting up a database with variables
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION