Use container host's IP:port if we're connected to a swarm master
This commit is contained in:
parent
ae85ec013b
commit
c4923d1f58
1 changed files with 23 additions and 7 deletions
30
nginx.tmpl
30
nginx.tmpl
|
@ -44,24 +44,40 @@ upstream {{ $host }} {
|
||||||
{{ $addrLen := len $container.Addresses }}
|
{{ $addrLen := len $container.Addresses }}
|
||||||
{{/* If only 1 port exposed, use that */}}
|
{{/* If only 1 port exposed, use that */}}
|
||||||
{{ if eq $addrLen 1 }}
|
{{ if eq $addrLen 1 }}
|
||||||
{{ with $address := index $container.Addresses 0 }}
|
{{ $address := index $container.Addresses 0 }}
|
||||||
# {{$container.Name}}
|
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
|
||||||
server {{ $address.IP }}:{{ $address.Port }};
|
{{ if and $container.Node $address.HostPort }}
|
||||||
|
# {{ $container.Node.Name }}/{{$container.Name}}
|
||||||
|
server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
|
||||||
|
{{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
|
||||||
|
{{ else }}
|
||||||
|
# {{$container.Name}}
|
||||||
|
server {{ $address.IP }}:{{ $address.Port }};
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
|
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
|
||||||
{{ else if $container.Env.VIRTUAL_PORT }}
|
{{ else if $container.Env.VIRTUAL_PORT }}
|
||||||
{{ range $address := .Addresses }}
|
{{ range $address := .Addresses }}
|
||||||
{{ if eq $address.Port $container.Env.VIRTUAL_PORT }}
|
{{ if eq $address.Port $container.Env.VIRTUAL_PORT }}
|
||||||
# {{$container.Name}}
|
{{ if and $container.Node $address.HostPort }}
|
||||||
server {{ $address.IP }}:{{ $address.Port }};
|
# {{ $container.Node.Name }}/{{$container.Name}}
|
||||||
|
server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
|
||||||
|
{{ else }}
|
||||||
|
# {{$container.Name}}
|
||||||
|
server {{ $address.IP }}:{{ $address.Port }};
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{/* Else default to standard web port 80 */}}
|
{{/* Else default to standard web port 80 */}}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
{{ $address := where $container.Addresses "Port" "80" | first }}
|
{{ $address := where $container.Addresses "Port" "80" | first }}
|
||||||
{{ if $address }}
|
{{ if $address }}
|
||||||
# {{$container.Name}}
|
{{ if and $container.Node $address.HostPort }}
|
||||||
server {{ $address.IP }}:80;
|
# {{ $container.Node.Name }}/{{$container.Name}}
|
||||||
|
server {{ $container.Node.Address.IP }}:{{ $address.HostPort }};
|
||||||
|
{{ else }}
|
||||||
|
# {{$container.Name}}
|
||||||
|
server {{ $address.IP }}:80;
|
||||||
|
{{ end }}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
# {{$container.Name}}
|
# {{$container.Name}}
|
||||||
server {{ $container.IP }} down;
|
server {{ $container.IP }} down;
|
||||||
|
|
Reference in a new issue