1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2024-11-22 20:06:30 +00:00

Support container in one network shared with current container

This commit is contained in:
Baptiste Donaux 2016-02-05 09:16:43 +01:00
parent 9341f54e51
commit 658e20f661

View File

@ -1,3 +1,5 @@
{{ $CurrentContainer := where $ "ID" .CurrentContainerID | first }}
{{ define "upstream" }} {{ define "upstream" }}
{{ if .Address }} {{ if .Address }}
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}} {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
@ -75,16 +77,24 @@ server {
upstream {{ $host }} { upstream {{ $host }} {
{{ range $container := $containers }} {{ range $container := $containers }}
{{ $addrLen := len $container.Addresses }} {{ $addrLen := len $container.Addresses }}
{{ $network := index $container.Networks 0 }}
{{/* If only 1 port exposed, use that */}} {{ range $knownNetwork := $CurrentContainer.Networks }}
{{ if eq $addrLen 1 }} {{ range $containerNetwork := $container.Networks }}
{{ $address := index $container.Addresses 0 }} {{ if eq $knownNetwork.Name $containerNetwork.Name }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $network) }} ## Can be connect with "{{ $containerNetwork.Name }}" network
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ else }} {{/* If only 1 port exposed, use that */}}
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }} {{ if eq $addrLen 1 }}
{{ $address := where $container.Addresses "Port" $port | first }} {{ $address := index $container.Addresses 0 }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $network) }} {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ else }}
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
} }