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

How Docker Compose references environment variables

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, Xiaobian will share with you how Docker Compose refers to relevant knowledge points of environmental variables. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you will gain something after reading this article. Let's find out together.

Note: The demo environment for this article is ubuntu 16.04.

compose cli and environment variables

compose cli(compose command-line, docker-compose program) can recognize environment variables named compose_project_name and compose_file (see for details of supported environment variables). For example, we can specify the project name and configuration file for docker-compose with these two environment variables:

$ export compose_project_name=testvar$ export compose_file=~/projects/composecounter/docker-compose.yml

Then launch the application, and the project name displayed is what we specified in the environment variable:

If environment variables are set and command-line options are specified, the command-line options settings apply:

$ docker-compose -p nickproject up -d

Reference environment variables in compose file

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

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

We specify the mirror image of the web via the environment variable ${imagetag}. Here is how to export the environment variable in the compose configuration file:

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

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

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

Thus, if the imagetag variable is not set, defaultwebimage is applied:

In addition to this, we can also set default values for environment variables through the.env file described later.

Passing environment variables to containers

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

web: environment: debug: 1

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

$ docker run -e debug=1

It is also easy to pass the value of the current shell environment variable to the container environment variable, just remove the assignment part of 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:

In the case of trying to export the environment variable debug:

$ export debug=1

This is the correct scenario we designed!

Use files to set multiple environment variables for containers

If setting environment variables for containers via environment isn't enough, we can also set environment variables for containers via files like docker -run's--env-file parameter:

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 envappname=helloworldauthor=nick liversion=1.0

Check the results:

Compose translates env_file into environment!

.env file

When we reference a large number of environment variables in docker-compose.yml, setting defaults for each environment variable would be cumbersome and would affect the brevity of docker-compose.yml. At this point we can set default values for all environment variables referenced by docker-compose.yml file via.env file!

Modify the contents of 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 an.env file in the same directory and edit its contents as follows:

# define env var default value.imagetag=defaultwebimageappname=default app nameauthor=default author nameversion=default version is 1.0

Check the results below, and all environment variables are 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 docker-compose.yml file with the following precedence:

compose file

shell environment variables

environment file

dockerfile

variable is not defined

First, the value set directly in the docker-compose.yml file has the highest priority.

Then the environment variable value exported in the current shell.

Next are the values defined in the environment variable file.

Then there are the values defined in dockerfile.

The environment variable is considered undefined if no relevant environment variable is found.

According to the above priority definition, we can define the environment variables in different scenarios in different shell scripts and export them, and then execute the source command before executing the docker-compose command to export the environment variables defined in the shell script to the current shell. In this way, we can reduce the maintenance of 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 contents of test.sh are as follows:

#!/ bin/bash# define env var default value.export imagetag=web:v1export appname=helloworldexport author=nick liexport version=1.0

prod.sh reads as follows:

#!/ bin/bash# define env var default value.export imagetag=webpord:v1export appname=helloworldprodexport author=nick liexport version=1.0lts

In the test environment, execute the following command:

$ source test.sh$ docker-compose config

The environment variables in docker-compose.yml are applied with settings relevant to the test environment.

In the production environment, execute the following command:

$ source prod.sh$ docker-compose config

The environment variables in docker-compose.yml are all production-related settings.

That's all for Docker Compose: How to Reference Environment Variables. Thanks for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report