Table of Contents
What are environment variables and why do you need them
In layman’s terms, environment variables are a collection of settings that can be passed to an application or service so the process running on a host machine can change. These settings can include things such as your database connection information, how many posts to index per page, or whether you want a blog post to display in RSS/Atom/JSON format. Environment variables can be used by any application or service that is running on a host machine. Docker is an application or service that runs in a container, and thus it too can make use of environment variables.
Generally, environment variables are used to tell an application or service which database settings to use in order to connect to your database. The syntax of the variables will likely follow the pattern: DB_HOST={DATABASE_HOSTNAME}; DB_PORT={DATABASE_PORT}; DB_USER={DATABASE_USERNAME}; and DB_PASSWORD={DATABASE_PASSWORD}. This pattern is designed to make the variables easier to read and remember.
In Docker, environment variables are used in a slightly different way. As opposed to the previous explanation, Docker environment variables are simply values passed to a Docker process by docker-compose.yml with the command:
docker-compose exec db_1 cat /etc/myapp/env/db_1_config.yml

How to set up environment variables for your docker-compose file
Creating environment variables for Docker-Compose files is easy: Open the docker-compose.yml file for editing. Navigate to the “services” section (where you set up your application or service containers). You just need to add a new key in that section called ENV. This can be any string, but it needs to fully match the key that you’re setting it to. Then add the values you want to pass. The values you add are set in the same order they are listed in the ENV section of docker-compose.yml . One value can contain multiple key-value pairs separated by a colon (i.e., “PATH=/opt/docker/bin:/opt/mysqld:3.7:/usr/local/mysql”). You can have multiple keys, and each key can have multiple values or none at all. In order to pass more than one key-value pair, separate them with spaces, but don’t add any spaces before the key or after the value (i.e., “PATH=/opt/docker/bin:/opt/mysqld:3.7” with no spaces would break the ENV line).
The ENV section is also where you specify custom ports for your applications. Just add a new port mapping like you would for the EXPOSE key.
Here’s an example docker-compose.yml file that has a custom environment variable and a custom port:
https://raw.githubusercontent.com/blackbeltio/blackbelt-php/master/docker-compose.yml

Examples of how to use environment variables in your docker-compose file
For example, if you’re on a Windows machine, you can point your MySQL client library to the WSL (Windows Subsystem for Linux) by adding key-value pairs like this:
MySQL_MYSQL_DATABASE=tcp://192.168.99.100:3306
MySQL_MYSQL_USERNAME=xxxx
MySQL_MYSQL_PASSWORD=xxxx
To find the list of environment variables for your working container, head over to “docker inspect” which will show you all the environment variables set for a particular container as well as any other information you’d like to see about that particular image. The full list is stored in a .env file in that container which is usually named after the project you are working with.
When running docker-compose, you can use environment variables too! In this case you may want to provide your own HTTP proxy server, and the explanation for why is probably obvious. IPv6 would also be a good choice here.

Tips for creating a more efficient and manageable docker-compose file
Here are some tips for how to make your docker-compose.yml file easier to manage and read: Group environment variables by its service. Grouping variables in this manner makes it easy to quickly see what environment settings are needed for a service and what settings it will require. For example, you might have one group for “db” for MySQL-specific db connection info and another group for “web” that holds information about your web server’s connection info.
Create a “common” group for any service specific data that is needed for all services and then use the “common” group to hold information about all of those common settings. For example, if you require your services to load from the same location, use the “common” group to hold that information so it can be referenced by every service.
Example:
compose “docker-psycorbine”: build: . docker-compose.yml: . – common: . volumes: .:/var/run/docker.sock:/var/run/docker.sock
