Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Explain in detail the problem of environment variables available in Docker Compose

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

1. Replace the environment variables in the Compose file

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

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

For more information, please refer to the Variable substitution section in the Compose documentation manual.

two。 Set environment variables in the container

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

Web: environment:-DEBUG=1

3. Pass environment variables to the container

Without assigning a value when using the environment keyword, you can pass the environment variable in shell to the service container, just like using docker run-e VARIABLE. The same:

Web: environment:-DEBUG

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

4. "env_file" configuration option

Multiple environment variables can be passed to the service container using external files through the env_file command, just like using docker run-- env-file=FILE... The same:

Web: env_file:-web-variables.env

5. Use 'docker-compose run' to set environment variables

Just like the docker run-e command, you can use docker-compose run-e to set the environment variables on the disposable container:

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

You can also pass a variable from shell instead of assigning a value 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 the 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 you can use it to configure Compose:

$cat .envTAG = v1.5$ cat docker-compose.ymlversion: '3'services: web: image: "webapp:$ {TAG}"

When running docker-compose up, the web service defined above uses the webapp:v1.5 image. You can print the configuration information of the application to the terminal through the config command to verify:

$docker-compose configversion: '3'services: web: image:' webapp:v1.5'

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

$export TAG=v2.0$ docker-compose configversion: '3'services: web: image:' webapp:v2.0'

When setting the same environment variable in multiple files, the following is the priority that Compose uses to select the value to use:

Compose file Environment file Dockerfile variable is not defined

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

$cat. / Docker/api/api.envNODE_ENV=test$ cat docker-compose.ymlversion: '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.

$docker-compose exec api nodeprocess.env.NODE_ENV'production'

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

Details of the NodeJS container

If you have a script's package.json entry that starts like NODE_ENV=test node server.js, this will override any settings in the docker-compose.yml file.

7. Configure Compose with environment variables

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

8. Create environment variables through link

When you use the links option in the first version of the Compose 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 creates an alias for the host instead.

Original address

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report