Example of how to reference environment variables in Docker Compose

Example of how to reference environment variables in Docker Compose

In a project, you often need to use environment variables in the docker-compose.yml file to control different conditions and usage scenarios. This article focuses on how Docker Compose references environment variables.

Note: The demonstration environment of this article is Ubuntu 16.04.

Compose CLI and environment variables

The Compose CLI (compose command-line, ie the docker-compose program) recognizes environment variables named COMPOSE_PROJECT_NAME and COMPOSE_FILE (see here for specific supported environment variables). For example, we can specify the project name and configuration file for docker-compose through these two environment variables:

$ export COMPOSE_PROJECT_NAME=TestVar
$ export COMPOSE_FILE=~/projects/composecounter/docker-compose.yml 

Then start the application, the project names displayed are those we specified in the environment variables:

If you set the environment variable and specify a command-line option at the same time, the command-line option setting will apply:

$ docker-compose -p nickproject up -d 

Referencing environment variables in the compose file

We can also directly reference environment variables in the compose file, such as the following demo:

version: '3'
 services:
 web:
  image: ${IMAGETAG}
  ports:
  - "5000:5000"
 redis:
  image: "redis:alpine"

We specify the web image through the environment variable ${IMAGETAG}. Now we pass the value of the environment variable in the compose configuration file through export:

Note that if the corresponding environment variable is not set, compose will replace it with an empty string:

In this case, we can set a default value for the variable in the compose configuration file:

version: '3'
services:
 web:
 image: ${IMAGETAG:-defaultwebimage}
 ports:
  - "5000:5000"
 redis:
 image: "redis:alpine"

Then, if the IMAGETAG variable is not set, the defaultwebimage will be applied:

In addition to this method, we can also set default values ​​for environment variables through the .env file that will be introduced later.

Passing environment variables to the container

Let's first look at how to set environment variables for the container in the compose file:

web:
 environment:
 DEBUG: 1

The environment node in the compose file is used to set environment variables for the container. The above is equivalent to:

$ docker run -e DEBUG=1

It is also very simple to pass the value of the current shell environment variable to the container's environment variable. Just remove the assignment part in the above code:

web:
 environment:
 DEBUG:

In this case, if the environment variable DEBUG is not exported in the current shell, it will be interpreted as null in the compose file:

When trying to export the environment variable DEBUG:

$ export DEBUG=1 

This is the correct usage scenario we designed!

Use files to set multiple environment variables for a container

If you think setting environment variables for the container through environment is not exciting enough, we can also set environment variables for the container through files like the --env-file parameter of docker-run:

web:
 env_file:
 -web-variables.env

Note that the path to the web-variables.env file is relative to the docker-compose.yml file. The above code has the same effect as the following code:

$ docker run --env-file=web-variables.env

One or more environment variables can be defined in the web-variables.env file:

#define web container env
APPNAME=helloworld
AUTHOR=Nick Li
VERSION=1.0

Check the results:

It turns out that compose translates the env_file settings into environment!

.env File

When we reference a large number of environment variables in the docker-compose.yml file, setting default values ​​for each environment variable will be cumbersome and will also affect the simplicity of docker-compose.yml. At this point we can use the .env file to set default values ​​for all environment variables referenced by the docker-compose.yml file!
Modify the contents of the docker-compose.yml file as follows:

version: '3'
services:
 web:
 image: ${IMAGETAG}     
 environment:
  APPNAME:
  AUTHOR:
  VERSION:
 ports:
  - "5000:5000"
 redis:
 image: "redis:alpine"

Then create a .env file in the same directory and edit its content as follows:

# define env var default value.
IMAGETAG=defaultwebimage
APPNAME=default app name
AUTHOR=default author name
VERSION=default version is 1.0

Check the results. All environment variables are now displayed with the default values ​​defined in the .env file:

Configure environment variables for different scenarios

As we can see from the previous section, Docker Compose provides enough flexibility for us to set the environment variables referenced in the docker-compose.yml file. Their priorities are as follows:

  1. Compose file
  2. Shell environment variables
  3. Environment file
  4. Dockerfile
  5. Variable is not defined

First, the value set directly in the docker-compose.yml file has the highest priority.
Then comes the environment variable value exported in the current shell.
Next are the values ​​defined in the environment variables file.
Next are the values ​​defined in the Dockerfile.
Finally, if the relevant environment variable is not found, it is considered that the environment variable is not defined.

According to the priority definition above, we can define the environment variables for different scenarios in different shell scripts and export them. Then, before executing the docker-compose command, execute the source command to export the environment variables defined in the shell script to the current shell. This way, you can reduce the number of places to maintain environment variables. In the following example, we create test.sh and prod.sh in the directory where the docker-compose.yml file is located. The content of test.sh is as follows:

#!/bin/bash
# define env var default value.
export IMAGETAG=web:v1
export APPNAME=HelloWorld
export AUTHOR=Nick Li
export VERSION=1.0

The content of prod.sh is as follows:

#!/bin/bash
# define env var default value.
export IMAGETAG=webpord:v1
export APPNAME=HelloWorldProd
export AUTHOR=Nick Li
export VERSION=1.0LTS

In the test environment, execute the following command:

$ source test.sh
$ docker-compose config 

At this time, the environment variables in docker-compose.yml apply settings related to the test environment.

In a production environment, execute the following command:

$ source prod.sh
$ docker-compose config 

At this time, the environment variables in docker-compose.yml apply settings related to the production environment.

Summarize

Docker Compose provides rich support and flexible usage of environment variables. I hope that the summary in this article can help you clarify the relevant usage and provide support for different usage scenarios in a concise way.

refer to:

Compose CLI environment variables
Environment variables in Compose
Compose file variable substitution
Declare default environment variables in file

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the available environment variables in Docker Compose
  • Use of environment variables in Docker and solutions to common problems

<<:  How does Vue3's dynamic components work?

>>:  How to create a stored procedure in MySQL and add records in a loop

Recommend

Nginx tp3.2.3 404 problem solution

Recently I changed Apache to nginx. When I moved ...

About the selection of time date type and string type in MySQL

Table of contents 1. Usage of DATETIME and TIMEST...

A pitfall and solution of using fileReader

Table of contents A pitfall about fileReader File...

How to use sed command to efficiently delete specific lines of a file

Preface Normally, if we want to delete certain li...

Brief Analysis of MySQL B-Tree Index

B-Tree Index Different storage engines may also u...

Analysis of examples of using anti-shake and throttling in Vue components

Be careful when listening for events that are tri...

How to set up vscode remote connection to server docker container

Table of contents Pull the image Run the image (g...

Creating private members in JavaScript

Table of contents 1. Use closures 2. Use ES6 clas...

MySQL single table query example detailed explanation

1. Prepare data The following operations will be ...

HTML Basics_General Tags, Common Tags and Tables

Part 1 HTML <html> -- start tag <head>...

SQL insert into statement writing method explanation

Method 1: INSERT INTO t1(field1,field2) VALUE(v00...

Zabbix implements monitoring of multiple mysql processes

Three MySQL instance processes are started on one...

React and Redux array processing explanation

This article will introduce some commonly used ar...

Learn the key knowledge that must be mastered in the Vue framework

1. What is Vue Vue is a progressive framework for...