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

Blog    

Recommend

How to convert a string into a number in JavaScript

Table of contents 1.parseInt(string, radix) 2. Nu...

Pure CSS to achieve click to expand and read the full text function

Note When developing an article display list inte...

How to monitor and delete timed out sessions in Tomcat

Preface I accidentally discovered that the half-h...

js drag and drop table to realize content calculation

This article example shares the specific code of ...

Tutorial on configuring and using i3 window manager in Linux

In this article, I will show you how to install a...

MySQL replication advantages and principles explained in detail

Replication is to transfer the DDL and DML operat...

A Preliminary Study on Vue Unit Testing

Table of contents Preface Why introduce unit test...

How MySQL uses transactions

Basics A transaction is an atomic operation on a ...

How to install php7 + nginx environment under centos6.6

This article describes how to install php7 + ngin...

Install ethereum/Ethereum from scratch under CentOS7

Table of contents Preface Add sudo write permissi...

Detailed explanation of the WeChat applet request pre-processing method

question Because some of our pages request data i...

How to add double quotes in HTML title

<a href="https://www.jb51.net/" titl...