Skip to content
tezvyn:
Menu

Environment Variables in Docker Compose

Source: docs.docker.comMediumHow cards are made

Environment variables are the runtime knobs for your Docker Compose services, letting you pass configuration like API keys or database URLs without rebuilding your image. Use them to connect services or set feature flags.

Why it exists

To decouple an application's configuration from its code. Hardcoding values like database hosts or API keys into a container image makes it inflexible and insecure. Environment variables allow the same image to be used across different stages (development, staging, production) simply by changing its runtime configuration.

The mental model

Think of your docker-compose.yml as a blueprint for a multi-service application. The environment section for each service is a set of instructions you pass to it at startup. It's like telling your web server container, 'Here is the address for the database,' without having to write that address into the server's source code.

How it works

Docker Compose provides several ways to set environment variables for a container, which are applied in a specific order of precedence. You can set them directly in the docker-compose.yml using the environment key as a list or a map. You can also use an env_file key to point to a file with variable definitions. Additionally, a file named .env in your project directory can be used to set default values for variables used within the Compose file itself. Finally, variables exported in your shell will override values in the .env file. Understanding this precedence is key to avoiding configuration conflicts.

When to use it

Use environment variables for any configuration that changes between deployments or environments. This includes database connection strings, external API endpoints, credentials for development, log level settings, and feature flags.

When not to use it

Avoid storing sensitive production secrets (like production database passwords or private API keys) directly in docker-compose.yml or .env files, especially if they are committed to version control. For production, use a dedicated secrets management tool like Docker Secrets, HashiCorp Vault, or a cloud provider's secret manager.

One canonical example

A web application service needs to connect to a database service. The docker-compose.yml defines both and uses environment variables to pass the database credentials to the web app.

services: db: image: postgres:14 environment: - POSTGRES_USER=myuser - POSTGRES_PASSWORD=mypassword web: build: . ports: ["8000:8000"] environment: - DB_HOST=db - DB_USER=myuser - DB_PASSWORD=mypassword

Here, the web service can use the DB_HOST, DB_USER, and DB_PASSWORD variables to construct its connection string.

Interview question

What type of configuration is generally NOT recommended to be managed directly via environment variables in docker-compose.yml or .env files for production deployments?

  • a.Hostnames or ports for internal service communication within the Compose network.
  • b.Database connection strings that vary between development and staging environments.
  • c.Feature flags that control application behavior in different environments.
  • d.Sensitive production credentials, such as private API keys or database passwords.Correct
Why?

The card explicitly advises against storing sensitive production secrets directly in docker-compose.yml or .env files for production, recommending dedicated secrets management tools instead. The other options describe valid and recommended uses for environment variables in Docker Compose.

Just read this? Test yourself on what you have been reading.

Read the original → docs.docker.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.

See open roles