From 55e66d658a5924a990312a7bd5e0e0183ddcb906 Mon Sep 17 00:00:00 2001 From: Timo Santi Date: Mon, 22 Feb 2016 23:40:00 +0200 Subject: [PATCH 1/2] Added support for networks Changed DOCKER_GEN_VERSION to 0.6.0 Needed newer version to support Networks struct --- Dockerfile | 2 +- nginx.tmpl | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8cb98cd..33873e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ RUN echo "daemon off;" >> /etc/nginx/nginx.conf \ RUN wget -P /usr/local/bin https://godist.herokuapp.com/projects/ddollar/forego/releases/current/linux-amd64/forego \ && chmod u+x /usr/local/bin/forego -ENV DOCKER_GEN_VERSION 0.4.2 +ENV DOCKER_GEN_VERSION 0.6.0 RUN wget https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \ && tar -C /usr/local/bin -xvzf docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \ diff --git a/nginx.tmpl b/nginx.tmpl index 255cc35..7e6b1ec 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -7,7 +7,7 @@ {{/* 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 }}; + server {{ .Network.IP }}:{{ .Address.Port }}; {{ end }} {{ else }} # {{ .Container.Name }} @@ -75,15 +75,16 @@ server { upstream {{ $host }} { {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} + {{ $network := index $container.Networks 0 }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} {{ $address := index $container.Addresses 0 }} - {{ template "upstream" (dict "Container" $container "Address" $address) }} + {{ template "upstream" (dict "Container" $container "Network" $network "Address" $address) }} {{/* 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) }} + {{ template "upstream" (dict "Container" $container "Network" $network "Address" $address) }} {{ end }} {{ end }} } From 0cb799d251d52111c4b502008645778682dc4f65 Mon Sep 17 00:00:00 2001 From: Timo Santi Date: Wed, 24 Feb 2016 09:04:28 -0500 Subject: [PATCH 2/2] Modified to work with older docker versions without network support --- nginx.tmpl | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 7e6b1ec..f87307e 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -7,7 +7,11 @@ {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}} {{ else }} # {{ .Container.Name }} - server {{ .Network.IP }}:{{ .Address.Port }}; + {{ if .Network }} + server {{ .Network.IP }}:{{ .Address.Port }}; + {{ else }} + server {{ .Address.IP }}:{{ .Address.Port }}; + {{ end }} {{ end }} {{ else }} # {{ .Container.Name }} @@ -75,16 +79,26 @@ server { upstream {{ $host }} { {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} - {{ $network := index $container.Networks 0 }} + {{ $networkLen := len $container.Networks }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} {{ $address := index $container.Addresses 0 }} - {{ template "upstream" (dict "Container" $container "Network" $network "Address" $address) }} + {{ if gt $networkLen 0 }} + {{ $network := index $container.Networks 0 }} + {{ template "upstream" (dict "Container" $container "Network" $network "Address" $address) }} + {{ else }} + {{ template "upstream" (dict "Container" $container "Address" $address) }} + {{ end }} {{/* 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 "Network" $network "Address" $address) }} + {{ if gt $networkLen 0 }} + {{ $network := index $container.Networks 0 }} + {{ template "upstream" (dict "Container" $container "Network" $network "Address" $address) }} + {{ else }} + {{ template "upstream" (dict "Container" $container "Address" $address) }} + {{ end }} {{ end }} {{ end }} }