Docker 从 env_file 组成 extra_hosts,而不是从默认的 .env 文件

2024-04-02

我正在使用 docker-compose 文件的版本 2。

是否可以从 env_file 读取 extra_hosts ?

我已经在一个环境变量文件中为所有 docker-compose 定义了公共变量。

样本 :-

version: '2'
workspace:
  env_file:
    - "../shared_environment/shared_variables.env"
  extra_hosts:
    - "dockerhost:${DOCKER_HOST_IP}"
    - "windowshost:${WINDOWS_HOST_IP}"

当我运行 docker-compose config 时,它有以下输出。

workspace:
environment:
  DOCKER_HOST_IP: 192.168.99.100
  WINDOWS_HOST_IP: 192.168.99.1
extra_hosts:
- 'dockerhost:'
- 'windowshost:'

因此,在上面的输出中,我可以看到它正在读取文件,但之后如何在 extra_hosts 中使用它?


The env_file配置将环境变量从外部文件传递到服务的容器。看“env_file”配置选项 https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option.

The extra_hosts在你的docker-compose.yml文件使用变量替换 https://docs.docker.com/compose/compose-file/#variable-substitution.

Compose 使用来自 shell 环境的变量值,其中 docker-compose 已运行。

为以下设备配置 IPextra_hosts,您可以将值设置为.env https://docs.docker.com/compose/environment-variables/#the-env-file归档或导出它们(例如export DOCKER_HOST_IP=192.168.99.100) 在 shell 中运行docker-compose.

您可以使用 .env 文件设置环境变量的默认值。 shell 环境中设置的值会覆盖 .env 中设置的值 文件。

内容.env file:

DOCKER_HOST_IP=192.168.99.100
WINDOWS_HOST_IP=192.168.99.1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker 从 env_file 组成 extra_hosts,而不是从默认的 .env 文件 的相关文章

随机推荐