在环境变量中设置额外的主机 [英] Set extra host in environment variables
问题描述
我正在使用docker compose运行我的应用程序。为此,我需要将主机设置在容器内(这取决于我运行的环境)。
我的方法是:
创建一个环境文件并设置变量:
#application.env
SERVER_IP = 10.10.9.134
我的docker compose文件如下:
版本:'2'
服务:
api:
container_name:myApplication
env_file:
-application.env
构建:./myApplication/
入口点:./docker/api-startup.sh
端口:
- 8080:8080
depends_on:
-redis
extra_hosts:& Extra_hosts
myip:$ SERVER_IP
但是我的问题是变量SERVER_IP从未被替换。
运行docker-compose config时,我看到:
services :
api:
构建:
上下文:/...../myApplication
container_name:myApplication
depend_on:
-Redis
入口点:./docker/api-startup.sh
环境:
SERVER_IP:10.10.9.134
extra_hosts:
myip:''
端口:
-8080:8080
我尝试使用$ SERVER_IP或$ {SERVER_IP}替换变量引用
我创建了一个文件 .env
,添加单行 HOST = test.example.com
,然后在docker-compose中执行以下操作:
额外主机:
-myip:$ {HOST}
docker-compose config
然后显示
extra_hosts:
myip :test.example.com
为此,我遵循了文档 Docker编写环境变量中有关 .env的部分的注释
文件
更新
根据Docker文档,
注:如果您的服务指定了构建选项,则在构建期间
环境文件中定义的变量将不会自动显示。
使用build的args子选项来定义构建时环境
变量。
如果将变量放在 .env
文件中,则可以在 docker-compose.yml
中使用它们进行替换,但是如果对特定容器使用 env_file
选项,则只能在Docker容器内看到变量,而不能在构建过程中看到。这也是合乎逻辑的, env_file
代替 docker run --env-file = FILE ...
而不是其他任何内容。 / p>
因此,您只能将值放入 .env
。另外,如William所述,您可以使用主机的环境变量。
I'm using docker compose to run my application. And for do that I need to set the hosts inside container (it's depends on the environment i'm running).
My approach was:
Create an environment file and set the variable:
#application.env
SERVER_IP=10.10.9.134
My docker compose file looks like:
version: '2'
services:
api:
container_name: myApplication
env_file:
- application.env
build: ./myApplication/
entrypoint: ./docker/api-startup.sh
ports:
- "8080:8080"
depends_on:
- redis
extra_hosts: &extra_hosts
myip: $SERVER_IP
But my problem is that the variable SERVER_IP is never replaced.
When I run docker-compose config I see:
services:
api:
build:
context: /...../myApplication
container_name: myApplication
depends_on:
- redis
entrypoint: ./docker/api-startup.sh
environment:
SERVER_IP: 10.10.9.134
extra_hosts:
myip: ''
ports:
- 8080:8080
I've tried to replace the variable reference using $SERVER_IP or ${SERVER_IP} but it didn't work.
I created a file .env
, added single line HOST=test.example.com
, then did this in docker-compose:
extra_hosts:
- myip:${HOST}
docker-compose config
then shows
extra_hosts:
myip: test.example.com
To do this I followed the documentation from Docker-compose environment variables the section about .env
file
UPDATE
According to the Docker documentation,
Note: If your service specifies a build option, variables defined in environment files will not be automatically visible during the build. Use the args sub-option of build to define build-time environment variables.
It basically means if you place your variables in .env
file, you can use them for substitution in docker-compose.yml
, but if you use env_file
option for the particular container, you can only see the variables inside the Docker container, not during the build. It is also logical, env_file
replaces docker run --env-file=FILE ...
and nothing else.
So, you can only place your values into .env
. Alternatively, as William described, you can use host's environment variables.
这篇关于在环境变量中设置额外的主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!