docker-gen
docker-gen copied to clipboard
Set proxy_pass from enviroment variable when starting container (nginx)
I need to set the proxy_pass in the template depending on whether an enviroment variable is set.
The problem i am having is I need to access the current $container object to get at the .Env object so I can access the enviroment variables. When you generate the server {} block in your nginx template, you are doing it outside of the loop that accesses the contents of each container, as you just need the host:
proxy_pass http://{{ trim $host }};
If I have an enviroment variable that is set when a container is run called PROXY_PASS, how can I check whether that is set, and if so set the proxy_pass to that, otherwise set it to $host?
I tried the following:
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
{{ range $index, $value := $containers }}
#{{ $value.Env.ALEX }}
{{ end }}
{{ end }}
Which works, however it prints out as many lines as there are containers (in this case just a comment to test if that works).
Maybe try something like: https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl#L102
Hi @jwilder took me a while to look at this again, so that looks like it would do the trick, however, in the nginx.tmpl file that comes with the nginx generator, it starts with this line:
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} I therefore thought I could put a line:
HOST: {{ $.Env.VIRTUAL_HOST }} below that line and it would output the VIRTUAL_HOST I have set. However that doesn't work (btw I realise that line would break nginx, this was just to test if I could access the VIRTUAL_HOST enviroment variable for the container.
Any ideas why it works in the range but not when I try to print it directly?