本文介绍了使用 Docker 的 12 要素配置方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何本机或普遍接受的方法来使用环境变量来控制 Docker 行为,即以 12 因素的方式?

Is there yet any native or commonly-accepted approaches to using environment variables to control Docker behaviour, i.e. in a 12factor manner?

我见过的唯一与语言无关的方法是使用 -e 变量污染 docker run 命令.我见过的最可维护的解决方案是使用 cat 和 sed 的组合使用 .env 文件生成 CLI 参数:https://twitter.com/DataKyle/status/422843345120296960

The only language-agnostic method I've seen is to pollute the docker run command with -e variables. The most maintainable solution I've seen is using a combination of cat and sed to generate the CLI parameters using a .env file: https://twitter.com/DataKyle/status/422843345120296960

我们目前使用 Vagrant for dev,这是一个用于测试和部署的 CI/CD 托管提供商,以及 AWS Elastic Beanstalk 作为暂存和生产 PAAS.我们的应用程序有 100 多个可配置参数,其中大部分设置为默认值,但每个环境仍需要自定义其中的 10-20 个左右.用大量的命令行变量来运行 docker 似乎太 hacky 了.

We currently use Vagrant for dev, a CI/CD hosted provider for test and deploy, plus AWS Elastic Beanstalk as the Staging and Production PAAS. Our app has over 100 configurable parameters, most of which are set to defaults, but each environment still needs to customise around 10-20 of those. It just seems too hacky to be running docker with a huge list of command-line variables like that.

此外,它不允许您从 docker 主机获取变量(例如 CI 提供商的预安装 Redis 或 Postgres 凭据),而无需进一步破解.

Further, it doesn't allow you to take variables from the docker host (such as the CI provider's pre-installed Redis or Postgres credentials), without a further hack.

我还没有找到解决方案吗?或者这是 Docker 的缺失部分?或者这在某种程度上违背了 Docker 哲学?

Is there a solution to this I haven't found?Or is this a missing piece for Docker?Or is this somehow philosophically against the Docker philosophy?

推荐答案

Docker 0.10.0 及更高版本(2014 年 4 月 8 日)接受 docker run --env-file ,它让您可以使用类似 .env 的文件来提供 docker 的运行环境.

Docker 0.10.0 and newer (Apr 8, 2014) accepts docker run --env-file <filename>, which lets you feed docker's running environment with .env-like files.

此外,还可以让 docker 进一步交互:--volumes-from 可以挂载引用容器中的所有卷,--link 让容器知道引用容器的暴露端口的详细信息.

Moreover, you can let dockers to interact further: --volumes-from can mount all volumes from the referenced container, and --link lets the container know the details of the referenced container's exposed ports.

虽然 Docker 运行参考 目前有点弱,但您可以找到所有CLI 参考的 运行部分中的详细信息,以及 容器链接参考.

While the Docker run reference is a bit weak at the moment, you can find all the details in the CLI reference's run section, as well as the container linking reference.

从容器开始.通常我建议启动一个 shell 脚本,它设置默认环境变量(沿着 : ${ENV:=default_value} 行),导出它们,然后 exec 一个单一的可执行文件.此可执行文件可以是前台所需的应用程序,也可以是 runitsupervisord 等初始化替换.

As of what to start from the container. Usually I recommend starting a shell script, which sets default environment variables (along the lines of : ${ENV:=default_value}), exports them, then execs a single executable. This executable can be the desired application in the foreground, or an init replacement like runit or supervisord.

我不建议在你的 docker 中运行 sshd,如果它不是系统的一部分(例如,gitlab 的容器应该包含 sshd,因为它通过 ssh 提供 git repo 访问).出于维护或调试目的,我建议改用 nsenter.

I wouldn't recommend running sshd inside your docker, if it's not part of the system (for example, a container for gitlab should contain sshd, as it provides git repo access through ssh). For maintenance or debugging purposes I recommend using nsenter instead.

这篇关于使用 Docker 的 12 要素配置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:38