What are environment variables and why do you need them for docker-compose
Environment variables are key-value pairs that provide a way to store configuration information outside of the container, but still manage it. They can be used as parameters for applications, or they can be used to define other aspects of the environment in which an app runs. This allows you to have a central place for settings that are meant to be shared across several containerized apps. You can use an environment variable to store the IP address of a web server that is run in multiple containers, for example.
For this article, we’re going to discuss the concepts of environment variables and how they differ from some of docker’s built-in options that are available within the docker-compose file. As we’ll see later in this article, these existing options may be sufficient for some use cases, but there are sometimes additional reasons for needing to configure your app using environment variables instead.
In Docker, you can pass environment variables in a few different ways. In a docker run command, you can include the name of an environment variable as one of the flags:
$ docker run -e MONGO_HOST = ” dbhost ” mongo
Or, in a docker-compose file, you can specify an environment variable using the syntax ENV VARIABLE_NAME “value”, where the value is what we want stored in the variable:
web: build: . ports: – “8000:8000″ environment: – MONGO_HOST = ” dbhost “
How to set up environment variables for docker-compose
Environment variables can be set at the OS level, using either your “/etc/environment” file or your shell’s built-in environment variables. Usually, you will want to do both. Keep in mind that you will have to restart your container to make the changes take effect.
If you want to use your shell’s built-in environment variables, you should edit your “.bash_profile” file in your home directory. If you use zsh, the file might be called “.zprofile” instead or somewhere else.
If you want to use your /etc/environment file, it must be executable and have “exec” (with no quotes) in its name. Example:
# Set using shell built-in environment variables [in my case in zsh] “bash-4.3$ source ~/.bash_profile” or # Set using user configuration file [“my environment”] “mysql-9.2$ echo . . .USER=mysql-service”
In the above example, “mysql-service” is the name of the mysql service that should be created by docker-compose.
After setting up environment variables for docker-compose, you can now run your mysql container using this command:
The different types of environment variables you can use with docker-compose
You can use both built-in and user defined environment variables with Docker-Compose. The only difference being whether or not they are global (which is the default) or local. These two classes of environment variables are used: Global environment variables – These are always present, so you won’t want to use these for any purposes. User defined environment variables – These are variables that you define within the stack definition section of the docker-compose file and they will only be present when the envvar is set in command line.
Built in Environment Variables
The following environment variables are created by Compose and are always present in the environment: COMPOSE_PROJECT_NAME – The single environment variable that Compose automatically includes is COMPOSE_PROJECT_NAME. This helps you organize your multi-container applications.
User Defined Environment Variables
You can define as many environment variables as you want by adding them to the env section of a service definition . For example, this requests an environment variable called “name”:
web : env_file : .env image : alpine command : [ “sh” , “-c” , “echo Hello from web && sleep infinity” ] env_file : .env name : myapp_web
Examples of how to use environment variables in your docker-compose files
Environment variables are often used to store sensitive data such as passwords and API keys for external services. So I am going to explain how to use environment variables with docker-compose in two different scenarios: a) Securing environment variables, b) Storing third party API keys.
Scenario 1) Securing environment variables
In this scenario we are going to use environment variables to store passwords. It can be any other kind of sensitive data you want to hide in your docker-compose files. You can add or omit the values you need based on your own needs.
A simple example:
mysql: image: mysql ports: – 3306 environment: MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
We are not using labels because we don’t want anyone to see this information in Docker Hub if we tag our container. Here, if you want to use another value for MYSQL_ROOT_PASSWORD, you can simply change it in the environment variable and then start your container with the new values.
Environment variables are automatically set against the image you have in your docker-compose file. This means they are not global and if you use multiple containers, they will be different per container.
Scenario 2) Storing third party API keys (“docker secret”) in a container