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..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 {{ .Address.IP }}:{{ .Address.Port }}; + {{ if .Network }} + server {{ .Network.IP }}:{{ .Address.Port }}; + {{ else }} + server {{ .Address.IP }}:{{ .Address.Port }}; + {{ end }} {{ end }} {{ else }} # {{ .Container.Name }} @@ -75,15 +79,26 @@ server { upstream {{ $host }} { {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} + {{ $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 "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 "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 }} }