Traefik配置文件的默认位置可以在官方Docker文件中更改吗?

Can the default location of the Traefik configuration file be changed in the official Docker file?

我有一个非关键的 Docker Compose 项目,其中 Traefik 规则在开发和生产之间的差异是可以接受的(我需要在产品上使用 Lets Encrypt,但在开发上不需要)。我正在使用 [file] 配置提供程序。

目前我正在为开发和生产创建单独的构建,因此:

# This is fetched from the Compose config
ARG BUILD_NAME

RUN if [ "$BUILD_NAME" == "prod" ]; then \
    echo Compiling prod config... ; \
    sh /root/compile/prod.sh > /etc/traefik/traefik.toml ; \
else \
    echo Compiling dev config... ; \
    sh /root/compile/dev.sh > /etc/traefik/traefik.toml ; \
fi

虽然这个项目不是非常重要,但每个环境的构建有点老套,我宁愿采用一个图像适用于所有环境的标准容器方法。

为此,我正在考虑做这样的事情:

FROM traefik:1.7

# This is set in the Docker Compose config
ENV ENV_NAME

# Let's have a sig handler
RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64
RUN chmod +x /usr/local/bin/dumb-init

COPY docker/traefik/start.sh /root/start.sh

ENTRYPOINT ["/usr/local/bin/dumb-init", "--"]
CMD ["/root/start.sh"]

start.sh 会在 运行 时 运行 我的 "compile" shell 命令(这会根据环境选择配置片段).但是,官方 Traefik 图像没有 运行 和 shell - 有一个来自 Go 源代码的编译 blob - 所以这是行不通的。是否有可以更改 /etc/traefik/traefik.toml 的环境变量,或者 Docker 中的行业标准方法?

我确实考虑过使用卷,但这意味着如果不进行额外设置,容器将不会 "plug-n-play" - 我喜欢它目前是独立的。但是,如果没有其他选择,我可能会使用它。我可以 运行 主机上的配置 "compiler"。

另一种方法是在具有 shell 的图像中安装 Traefik - 也许它适用于 Alpine。我不确定我对此有何看法 - 删除 shell 是一个很好的安全功能,所以我犹豫是否要重新添加它,即使我认为它不容易被利用。

我没有找到使用环境变量修改 Traefik 配置文件路径的方法。但是,我找到了一个似乎非常独立的基于卷的解决方案。

  1. 我在 Docker Compose 文件中设置了另一个名为 shell 的图像:

    shell:
      build:
        context: docker/shell
      volumes:
        # On-host volume for generating config
        - "./docker/traefik/compiled:/root/compiled-host"
    

    这有一个绑定安装卷来捕获生成的配置文件。

  2. 接下来,我为新服务创建了一个Dockerfile

    FROM alpine:3.10
    
    COPY compile /root/compile
    COPY config /root/config
    
    # Compile different versions of the config, ready to copy into an on-host volume
    RUN mkdir /root/compiled && \
        sh /root/compile/dev.sh > /root/compiled/traefik-dev.toml && \
        sh /root/compile/prod.sh > /root/compiled/traefik-prod.toml
    

    这将为两个环境创建配置文件作为构建映像的一部分。

  3. 当 Docker Compose 启动时,该服务会短暂启动,但很快就会优雅且无害地退出。无论如何,它都是临时 运行。

  4. 我已经有了特定于环境的 YAML 配置文件,docker-compose-dev.ymldocker-compose-prod.yml,它们在 Compose 命令中用 -f 明确指定。然后我使用这个文件将生成的主机上文件公开给 Traefik。这是开发人员:

      traefik:
        volumes:
          # On-host volume for config
          - "./docker/traefik/compiled/traefik-dev.toml:/etc/traefik/traefik.toml"
    

    traefik-prod.toml

  5. 也做了很多相同的事情
  6. 然后,我创建了 per-env 命令以将配置从 shell 映像复制到主机上的卷中:

    #!/bin/sh
    
    docker-compose -f docker-compose.yml -f docker-compose-prod.yml run shell cp /root/compiled/traefik-prod.toml /root/compiled-host/traefik-prod.toml
    
  7. 最后,当 Traefik 作为 Compose 应用程序的一部分启动时,它会在通常的位置找到它的配置文件,/etc/traefik/traefik.toml,但这实际上是生成的文件卷在主机上复制。