Elastic Beanstalk environment variables for Docker host

后端 未结 5 675
春和景丽
春和景丽 2021-02-08 04:51

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 varia

5条回答
  •  清歌不尽
    2021-02-08 05:30

    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]}\n" if pair[1]
    end
    puts envs
    

    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.

提交回复
热议问题