Skip to content

Instantly share code, notes, and snippets.

@geetfun
Created November 19, 2014 06:09
Show Gist options
  • Save geetfun/ed7d07769191acc8e9d6 to your computer and use it in GitHub Desktop.
Save geetfun/ed7d07769191acc8e9d6 to your computer and use it in GitHub Desktop.
Ruby shell script to generate nginx.conf from docker containers
upstream propertypub {
<% ip_addresses_for_web_containers.each do |ip_address| %>
server <%= ip_address %>:80;
<% end %>
}
#!/usr/bin/env ruby
require 'erb'
containers = `docker ps`
containers_array = containers.split
containers = containers_array.select { |string| string.include?('app_web_') }
ip_addresses_for_web_containers = []
if !containers.empty?
containers.each do |c|
ip_address = `docker inspect #{c} | grep IPAddress | cut -d '"' -f 4`
ip_address = ip_address.delete("\n")
ip_addresses_for_web_containers << ip_address
end
end
conf_template = File.read("nginx.conf.erb")
erb = ERB.new(conf_template)
conf = erb.result(binding)
File.open("nginx.conf", "w") do |file|
file.puts(conf)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment