---
title: Using Environment Variables with Docker Compose
description: A quick reference guide for using environment variables with Docker Compose.
date: 2019-11-25
tags: docker, docker-compose, environment variables
source: https://brianchildress.co/blog/environment-variables-docker-compose
---

# Using Environment Variables with Docker Compose

[Docker Compose](https://docs.docker.com/compose/) is a powerful tool for orchestrating your [Docker](https://www.docker.com/) environment. Managing environment information can be easier to do if you setup your Docker configuration in the following way.

Create a _docker-compose.yml_ file

```yml
version: '3'
services:
  api:
    environment:
      - DB_USER=${DB_USER_NAME}
      - DB_PASS=${DB_PASSWORD}
      ...
```

Then in an _.env_ file, in the same directory, you can define your environment specific values.  

_**DO NOT CHECK THIS FILE INTO VERSION CONTROL!!!!**_

```
DB_USER_NAME=mydbuser
DB_PASSWORD=mysecretdbpassword
...
```

With the _.env_ file in the same directory as your _docker-compose.yml_ file, Docker Compose will substitute the values in your environment variables with the corresponding values in the _.env_ file that specific for each environment. This allows your _docker-compose.yml_ file to remain consistent without the possibility of potentially exposing sensitive information through your _docker-compose.yml_ file.

_**NOTE:** It is recommended that you use a secrets management solution like [Hashicorp's Vault](https://www.vaultproject.io/) to manage sensitive information like credentials._
