0
0
Dockerdevops~5 mins

Environment variables in Compose in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Environment variables let you set values outside your app code so you can change settings easily. In Docker Compose, they help you configure containers without changing the Compose file itself.
When you want to set a database password without writing it directly in the Compose file.
When you need to run the same app in different environments like development and production with different settings.
When you want to share common settings like ports or API keys across multiple services in Compose.
When you want to keep sensitive information out of version control but still use it in your containers.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  webapp:
    image: nginx:1.23
    ports:
      - "8080:80"
    environment:
      - APP_ENV=production
      - API_KEY=${API_KEY}

This Compose file defines a service named webapp using the nginx image.

The environment section sets two variables: APP_ENV is set directly to 'production', and API_KEY is set from the host environment variable API_KEY.

This allows you to keep sensitive keys outside the file and inject them when running Compose.

Commands
This command sets the environment variable API_KEY in your shell so Docker Compose can use it.
Terminal
export API_KEY=12345secretkey
Expected OutputExpected
No output (command runs silently)
This command starts the services defined in docker-compose.yml in detached mode, applying the environment variables.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating webapp ... done
-d - Run containers in the background (detached mode)
This command runs printenv inside the running webapp container to show the values of the environment variables.
Terminal
docker-compose exec webapp printenv APP_ENV API_KEY
Expected OutputExpected
production 12345secretkey
This command stops and removes the containers, networks, and other resources created by docker-compose up.
Terminal
docker-compose down
Expected OutputExpected
Stopping webapp ... done Removing webapp ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: environment variables let you change container settings without editing the Compose file.

Common Mistakes
Not exporting the environment variable in the shell before running docker-compose up.
Docker Compose cannot substitute the variable if it is not set in the shell, so the container gets an empty or missing value.
Always export the needed environment variables in your shell before running docker-compose up.
Writing environment variables directly in the Compose file for sensitive data like passwords.
This exposes secrets in version control and shared files, risking security.
Use shell environment variables or .env files ignored by version control to keep secrets safe.
Using incorrect syntax like APP_ENV: production instead of the list format or key-value pairs under environment.
Docker Compose expects environment variables in a specific format; wrong syntax causes errors or ignored variables.
Use either a list with - VAR=value or a map with VAR: value under the environment key.
Summary
Set environment variables in your shell to pass dynamic values to Docker Compose.
Use the environment section in docker-compose.yml to define variables for containers.
Verify variables inside containers using docker-compose exec and printenv.
Stop and clean up containers with docker-compose down.