简体   繁体   English

docker-compose 内插环境变量:使用 docker-compose 提供的默认变量

[英]docker-compose interpolate environment variables: use default variables provided by docker-compose

I need some help with the following template:我需要以下模板的帮助:

services:

nginx:
  image: nginx
  restart: unless-stopped
  labels:
    - "traefik.enable=true"
    - "traefik.http.routers.nginx-${COMPOSE_PROJECT_NAME}.rule=Host(`fuu.bar`)"
  networks:
    - treafik

My goal is to create a template which I can use eg in portainer with almost zero configuration.我的目标是创建一个模板,我可以在几乎零配置的情况下使用它。

I thought that the following variables are available in docker-compose config but the expression ${COMPOSE_PROJECT_NAME} results in an empty string: docker-compose config我认为以下变量${COMPOSE_PROJECT_NAME} -compose config 中可用,但表达式${COMPOSE_PROJECT_NAME}导致空字符串: ${COMPOSE_PROJECT_NAME} docker-compose config

services:

nginx:
  image: nginx
  restart: unless-stopped
  labels:
    - "traefik.enable=true"
    - "traefik.http.routers.nginx-.rule=Host(`fuu.bar`)"
  networks:
    - treafik

Are there any default environment variables provided by docker-compose which I can use for environment interpolation? docker-compose 是否提供了任何可用于环境插值的默认环境变量?

---- Update - - 更新

I use traefik (v2) as a reverse proxy.我使用 traefik (v2) 作为反向代理。 To make the containers available through treafik, you need to define routers on every service.要通过 treafik 使容器可用,您需要在每个服务上定义路由器。 The router name has to be unique.路由器名称必须是唯一的。 Lets imagine you deploy 2 or more stacks of the above template.假设您部署了上述模板的 2 个或更多堆栈。 The router name has to be unique for all services across all stacks.对于所有堆栈中的所有服务,路由器名称必须是唯一的。 Because Im a lazy guy, I tried to simply integrate the environment variable COMPOSE_PROJECT_NAME (which I know is already unique in my setup because every stack must have a unique name).因为我是个懒人,所以我尝试简单地集成环境变量COMPOSE_PROJECT_NAME (我知道它在我的设置中已经是唯一的,因为每个堆栈都必须有一个唯一的名称)。 But the variable is not available when deploying the stack.但是在部署堆栈时该变量不可用。 Of course, I could simply define the variable COMPOSE_PROJECT_NAME by myself in a .env-file, but i hoped that there are any default environment variables provided by docker.当然,我可以简单地在 .env 文件中自己定义变量COMPOSE_PROJECT_NAME ,但我希望有 docker 提供的任何默认环境变量。

You can use environment variables to passing strings to your docker file.您可以使用环境变量将字符串传递给您的 docker 文件。 There are many ways through docker documentation .通过docker 文档有很多方法。 For example:例如:

You can set default values for any environment variables referenced in the Compose file, or used to configure Compose, in an environment file named .env.您可以在名为 .env 的环境文件中为 Compose 文件中引用或用于配置 Compose 的任何环境变量设置默认值。 The .env file path is as follows: .env 文件路径如下:

  • Starting with +v1.28, .env file is placed at the base of the project directory从 +v1.28 开始,.env 文件放置在项目目录的底部
  • Project directory can be explicitly defined with the --file option or COMPOSE_FILE environment variable.可以使用 --file 选项或 COMPOSE_FILE 环境变量显式定义项目目录。 Otherwise, it is the current working directory where the docker compose command is executed (+1.28).否则,它是执行 docker compose 命令的当前工作目录(+1.28)。
  • For previous versions, it might have trouble resolving .env file with --file or COMPOSE_FILE.对于以前的版本,它可能无法使用 --file 或 COMPOSE_FILE 解析 .env 文件。 To work around it, it is recommended to use --project-directory, which overrides the path for the .env file.要解决此问题,建议使用 --project-directory,它会覆盖 .env 文件的路径。 This inconsistency is addressed in +v1.28 by limiting the filepath to the project directory. +v1.28 通过将文件路径限制为项目目录来解决此不一致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM