Detailed explanation of the available environment variables in Docker Compose

Detailed explanation of the available environment variables in Docker Compose

Several parts of Compose deal with environment variables in some way. This tutorial can help you find the information you need.

1. Replace environment variables in Compose file

You can populate values ​​in a Compose file using environment variables from your shell:

web:
 image: "webapp:${TAG}"

For more information, see the Variable substitution section in the Compose file manual.

2. Set environment variables in the container

You can set environment variables in the service container using the environment keyword, just like using docker run -e VARIABLE=VALUE ...:

web:
 environment:
 -DEBUG=1

3. Passing environment variables to the container

When using environment keyword without assigning a value, you can pass the environment variables in the shell to the service container, just like using docker run -e VARIABLE ... :

web:
 environment:
 -DEBUG

The value of the DEBUG variable in the container is taken from the variable of the same name in the shell that runs Compose.

4. The “env_file” configuration option

You can use the env_file command to pass multiple environment variables to the service container using an external file, just like using docker run --env-file=FILE ... :

web:
 env_file:
 -web-variables.env

5. Set environment variables using 'docker-compose run'

Just like docker run -e command, you can use docker-compose run -e to set environment variables on a one-off container:

docker-compose run -e DEBUG=1 web python console.py

You can also pass a variable from the shell instead of assigning it directly:

docker-compose run -e DEBUG web python console.py

The value of the DEBUG variable in the container is taken from the variable of the same name in the shell that runs Compose.

6. “.env” File

You can set default values ​​for any environment variables referenced in a Compose file in an environment file named .env, or used to configure Compose:

$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: '3'
services:
 web:
 image: "webapp:${TAG}"

When running docker-compose up , the web service defined above uses webapp:v1.5 image. You can verify the application's configuration information by printing it to the terminal using the config command:

$ docker-compose config

version: '3'
services:
 web:
 image: 'webapp:v1.5'

Values ​​in the shell take precedence over values ​​specified in the .env file. If you set TAG to a different value in the shell, that value will be used in the image:

$ export TAG=v2.0
$ docker-compose config

version: '3'
services:
 web:
 image: 'webapp:v2.0'

When setting the same environment variable in multiple files, here is the precedence Compose uses to choose which value to use:

  • Compose file
  • Environment File
  • Dockerfile
  • Variable is not defined

In the following example, we set the same environment variables in both the Environment file and the Compose file:

$ cat ./Docker/api/api.env
NODE_ENV=test

$ cat docker-compose.yml
version: '3'
services:
 API:
 image: 'node:6-alpine'
 env_file:
  - ./Docker/api/api.env
 environment:
  - NODE_ENV=production

When running a container, environment variables defined in the Compose file take precedence.

$ docker-compose exec api node

process.env.NODE_ENV
'production'

Any ARG or ENV settings in Dockerfile are only evaluated if there is no Docker Compose entry for environment or env_file .

NodeJS container details

If you have a package.json entry for your script to start like NODE_ENV=test node server.js then this will override any settings in your docker-compose.yml file.

7. Configure Compose using environment variables

There are several environment variables that can be used to configure the behavior of the Docker Compose command line. They begin with COMPOSE_ or DOCKER_ and are recorded in the CLI environment variables.

8. Create environment variables through link

When using the links option in the first version of the Compose file, environment variables are created for each link. They are documented in the Link environment variable reference.

However, these variables have been deprecated. link creates an alias for the host instead.

Original URL

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:
  • Example of how to reference environment variables in Docker Compose
  • Use of environment variables in Docker and solutions to common problems

<<:  Detailed explanation of how to use the canvas operation plugin fabric.js

>>:  Share 101 MySQL debugging and optimization tips

Recommend

innerHTML Application

Blank's blog: http://www.planabc.net/ The use...

A brief analysis of the basic implementation of Vue detection data changes

Table of contents 1. Object change detection 2. Q...

How to modify the IP restriction conditions of MySQL account

Preface Recently, I encountered a requirement at ...

Summary of clipboard.js usage

Table of contents (1) Introduction: (2) The ways ...

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

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

Detailed explanation of nginx-naxsi whitelist rules

Whitelist rule syntax: BasicRule wl:ID [negative]...

Example code of how CSS matches multiple classes

CSS matches multiple classes The following HTML t...

A brief discussion on Nginx10m+ high concurrency kernel optimization

What is high concurrency? The default Linux kerne...

Detailed tutorial on how to automatically install CentOS7.6 using PXE

1. Demand The base has 300 new servers, and needs...

Implement group by based on MySQL to get the latest data of each group

Preface: The group by function retrieves the firs...

Analysis of the cause of docker error Exited (1) 4 minutes ago

Docker error 1. Check the cause docker logs nexus...

Vue implements horizontal beveled bar chart

This article shares the specific code of Vue to i...

Complete steps to quickly configure HugePages under Linux system

Preface Regarding HugePages and Oracle database o...