Detail the environment variables available in Docker Compose

  • 2020-12-20 03:53:35
  • OfStack

Multiple parts of Compose deal with environment variables in some cases. This tutorial will help you find the information you need.

1. Replace environment variables in the Compose file

You can populate the values in the Compose file with the environment variables in shell:


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

Refer to the Variable substitution section of the Compose Documentation manual for more information.

2. Set the environment variables in the container

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


web:
 environment:
 - DEBUG=1

3. Pass environment variables to the container

In the use of environment The environment variable in shell can be passed to the service container without assigning a value to the keyword, as follows docker run -e VARIABLE ... 1:


web:
 environment:
 - DEBUG

The value of the DEBUG variable in the container is obtained from the same name variable in shell running Compose.

4. "env_file" configuration option

Can be achieved by env_file The command uses an external file to pass multiple environment variables to the service container, as follows docker run --env-file=FILE ... 1:


web:
 env_file:
 - web-variables.env

5. Use 'ES54en-ES55en run' to set the environment variable

Just like docker run -e Command 1 like, can be used docker-compose run -e Set the environment variable on the first-order container:


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

You can also do this by passing a variable from 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 obtained from the variable of the same name in shell running Compose.

6. ". env "file

You can set the default value for any environment variable referenced in the Compose file in an environment file named.env, or use it to configure Compose:


$ cat .env
TAG=v1.5

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

run docker-compose up , defined above web Service use webapp:v1.5 The mirror. You can verify this by printing the configuration information of the application to the terminal using the config command:


$ docker-compose config

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

Values in shell take precedence over .env The value specified in the file. If TAG is set to a different value in shell, that value will be used in the mirror:


$ 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 priority that Compose uses to select the values to use:

Compose file Environment file Dockerfile Variables undefined

In the following example, we set the same environment variable on the Environment file and 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 the container, the environment variables defined in the Compose file take precedence.


web:
 environment:
 - DEBUG=1
0

Only in the docker run -e VARIABLE ... 0 or env_file When there is no Docker Compose entry, Dockerfile Any of the ARG or ENV Settings are evaluated only (evaluate).

[

NodeJS container details

If you have a scripted ES127en.json entry launched like NODE_ENV=test node server.js 1, this will override any Settings in the ES135en-ES136en.yml file.

]

7. Configure Compose using environment variables

There are several environment variables you can use to configure the Docker Compose command line behavior. They begin with COMPOSE_ or DOCKER_ and are recorded in the CLI environment variable.

8. Create environment variables with link

When you use the links option in the Compose version 1 file, an environment variable is created for each link. They are recorded in the Link environment variable reference.

However, these variables have been deprecated. link changed to create aliases for hosts.

The original address


Related articles: