How to use Docker .env file

Make your docker-compose.yml composition easier and cleaner by declaring variables in an .env file.

How to use Docker .env file Make your docker-compose.yml composition easier and cleaner by declaring variables in an .env file.

The Docker environment variable file (.env) is crucial when you're creating complex container deployments. As you might expect from the name, this file allows you to declare environment variables for your containers. This comes in quite handy, as the .env file can be reused for other containers or quickly edited--instead of having to edit the more complex docker-compose.yml files.

I want to show you the basics of using the .env file.

SEE: Hybrid cloud: A guide for IT pros (TechRepublic download)

What you'll need

In order to make use of .env files, you'll need a working instance of Docker and docker-compose. 

Declaring values in the .env file

Within the .env file, you declare values for certain variables. These values are in the form:

VARIABLE_NAME=VALUE

Where VARIABLE_NAME is the name of the variable and VALUE is the actual value for the variable.

These value pairs (within the .env file) are used in place of declaring variables in the docker-compose.yml file, which are in the form:

environment:
- VARIABLE_NAME=${VALUE}

When you hard-code those variables in the docker-compose.yml file, they cannot be reused for other containers, and they must be edited directly if changed.

Within the .env file, you can declare just about any kind of variable. For example, if you're using a database for your container. In the .env file you can declare:

  • DB_NAME
  • DB_USER
  • DB_PW
  • DB_ROOT_PW

So let's say you have your .env file (that's the full name of the file, by the way) located in the same directory that houses your docker-compose.yml file. In that .env file, you have on these lines:

DB_NAME=dbase
DB_USER=dbuser
DB_PW=dbpassword
DB_ROOT_PW=dbrootpw

Using variables

With those variables declared, how do you make use of them? From within the docker-compose.yml file, you can then call them like so:

services:
    db:
      image: mysql
      environment:
       - MYSQL_DATABASE: "${DB_NAME}"
       - MYSQL_USER: "${DB_USER}"
       - MYSQL_ROOT_PASSWORD: "${DB_ROOT_PW}"
       - MYSQL_PASSWORD: "${DB_PW}"

Since you've defined those variables in the .env file, they will automatically be read when you issue the command docker-compose up, as the docker-compose command always checks for the .env file first. 

Assuming you have a fully fleshed-out docker-compose.yml file, your container should build without error.

Why this method?

Using the .env file allows you to take a write-once-use-often approach to configuring your containers. Although you might not use the exact same variables for various containers, it allows you to create a single .env file and then easily edit the values, so it can be repurposed for other containers. This also makes for easier writing of docker-compose.yml files, as you're not having to hard-code all environment variables. 

Give this method a try and see if it doesn't help make your docker-compose.yml writing a bit easier and more efficient.

Also see

dockerhero.jpg

Image: Docker