本文介绍了Docker 主机的 Elastic Beanstalk 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正确部署了 Docker Web 应用程序 (rails) 的 EB 环境.我设置了几个 EB 环境变量,它们在容器中正确可见.现在 - 我希望这些 EB 环境变量对 EC2 实例主机可见,以便我可以在 docker 构建过程中使用它们.但是,它们不会暴露给 docker 主机,只会暴露给容器.
如何向 Docker 主机公开 EB 环境变量?

I have an EB env with Docker web app (rails) properly deployed. I set several EB env variables and they are properly visible in the container. Now - I'd like these EB env variables to be visible to the EC2 instance host, so that I can use them in the docker build process. However, they are not exposed to the docker host, only to the container.
How do I expose EB env variables to the Docker host?

推荐答案

这是一个问题,所以我将我的解决方案发布给遇到这个问题的人.
Elastic Beanstalk Docker 实例不会向 docker 主机公开环境变量.它只对 docker 容器执行此操作.
如果您想获取主机上的环境变量,它们位于 /opt/elasticbeanstalk/deploy/configuration/containerconfiguration.
这是一个大型 JSON 文件,很方便违反了环境变量的 JSON 结构.
我写了一个小的 ruby​​ 脚本来解析它并从中提取环境变量:

This was a though one, so I'm posting my solution for those who encounter this.
Elastic Beanstalk Docker instance does not expose the environment variables to the docker host. It does that only to the docker container.
If you'd like to get the env variables on the host, they are located at /opt/elasticbeanstalk/deploy/configuration/containerconfiguration.
This is one large JSON file, conveniently disobeying the JSON structure for the env vars.
I wrote a small ruby script to parse it and extract the env vars from it:

require 'json'
container_config = JSON.parse(File.read('/opt/elasticbeanstalk/deploy/configuration/containerconfiguration'))
raw_vars =  container_config['optionsettings']['aws:elasticbeanstalk:application:environment']
envs = ''
raw_vars.each do |raw_var|
  pair = raw_var.split('=')
  envs << "export #{pair[0]}=#{pair[1]}
" if pair[1]
end
puts envs

此脚本生成一组导出命令到设置环境变量的控制台.我对其进行了一些调整,将 ENV 命令写入我的 Dockerfile.

this script yields a set of export commands to console that sets the env vars. I adapted it a bit to write ENV commands into my Dockerfile.

这篇关于Docker 主机的 Elastic Beanstalk 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 07:31