Skip to content

Commit 01cf75c

Browse files
committed
update for docker networking layer
This fixes issues with docker-compose containers killing the proxy. I'm using a fork of docker-gen while I wait for the groupByTag PR to be merged and released. I've strayed further from the upstream nginx-proxy container now, I'm not sure that it makes sense to build on top of it anymore. These changes aren't likely to be accepted upstream since they're targetted specifically at a local dev environment, which is not a primary use case for nginx-proxy.
1 parent 39198df commit 01cf75c

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

Dockerfile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
FROM jwilder/nginx-proxy
1+
FROM jwilder/nginx-proxy:0.3.6
22
MAINTAINER Brian Palmer <[email protected]>
33

4-
ADD *.conf /etc/nginx/conf.d/
4+
# XXX: this can be removed once upstream nginx-proxy is updated
5+
ENV DOCKER_GEN_VERSION 0.6-groupByLabel
6+
RUN wget https://github.com/codekitchen/docker-gen/releases/download/v0.6-groupByLabel/docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
7+
&& tar -C /usr/local/bin -xvzf docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
8+
&& rm docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz
9+
# /XXX
10+
11+
# override nginx configs
12+
COPY *.conf /etc/nginx/conf.d/
13+
14+
# override nginx-proxy templating
15+
COPY nginx.tmpl Procfile reload-nginx /app/

Procfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nginx: nginx
2+
dockergen: docker-gen -watch -only-exposed -notify "/app/reload-nginx" /app/nginx.tmpl /etc/nginx/conf.d/default.conf

nginx.tmpl

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{{ $CurrentContainer := where $ "Hostname" .Env.HOSTNAME | first }}
2+
3+
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
4+
# scheme used to connect to this server
5+
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
6+
default $http_x_forwarded_proto;
7+
'' $scheme;
8+
}
9+
10+
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
11+
# Connection header that may have been passed to this server
12+
map $http_upgrade $proxy_connection {
13+
default upgrade;
14+
'' close;
15+
}
16+
17+
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
18+
19+
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
20+
'"$request" $status $body_bytes_sent '
21+
'"$http_referer" "$http_user_agent"';
22+
23+
access_log off;
24+
25+
{{ if (exists "/etc/nginx/proxy.conf") }}
26+
include /etc/nginx/proxy.conf;
27+
{{ else }}
28+
# HTTP 1.1 support
29+
proxy_http_version 1.1;
30+
proxy_buffering off;
31+
proxy_set_header Host $http_host;
32+
proxy_set_header Upgrade $http_upgrade;
33+
proxy_set_header Connection $proxy_connection;
34+
proxy_set_header X-Real-IP $remote_addr;
35+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36+
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
37+
{{ end }}
38+
39+
{{ define "server" }}
40+
{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
41+
{{ $proto := or (first (groupByKeys .Containers "Env.VIRTUAL_PROTO")) "http" }}
42+
43+
upstream {{ .Host }} {
44+
{{ range $container := .Containers }}
45+
{{ $port := coalesce $container.Env.VIRTUAL_PORT (first $container.Addresses).Port "80" }}
46+
{{ $address := coalesce (first $container.Addresses).IP (first $container.Networks).IP }}
47+
server {{ $address }}:{{ $port }};
48+
{{ end }}
49+
}
50+
51+
server {
52+
server_name {{ .Host }};
53+
listen 80;
54+
access_log /var/log/nginx/access.log vhost;
55+
56+
{{ if (exists (printf "/etc/nginx/vhost.d/%s" .Host)) }}
57+
include {{ printf "/etc/nginx/vhost.d/%s" .Host }};
58+
{{ else if (exists "/etc/nginx/vhost.d/default") }}
59+
include /etc/nginx/vhost.d/default;
60+
{{ end }}
61+
62+
location / {
63+
proxy_pass {{ trim $proto }}://{{ trim .Host }};
64+
{{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
65+
auth_basic "Restricted {{ .Host }}";
66+
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
67+
{{ end }}
68+
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
69+
include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
70+
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
71+
include /etc/nginx/vhost.d/default_location;
72+
{{ end }}
73+
}
74+
}
75+
{{ end }}
76+
77+
{{ $explicit := whereExist $ "Env.VIRTUAL_HOST" }}
78+
{{ range $host, $containers := groupByMulti $explicit "Env.VIRTUAL_HOST" "," }}
79+
{{ template "server" (dict "Containers" $containers "Host" $host) }}
80+
{{ end }}
81+
82+
{{ range $project, $projContainers := groupByLabel $ "com.docker.compose.project" }}
83+
{{ range $service, $containers := groupByLabel $projContainers "com.docker.compose.service" }}
84+
{{ $container := first $containers }}
85+
{{ $host := printf "%s.%s.%s" $service $project "docker" }}
86+
{{ template "server" (dict "Containers" $containers "Host" $host) }}
87+
{{ end }}
88+
{{ end }}

reload-nginx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
set -e
3+
4+
name=${CONTAINER_NAME:-dinghy_http_proxy}
5+
6+
# Join any networks we haven't joined yet, so that we can talk to containers on
7+
# those networks. This allows us to talk to containers created with
8+
# docker-compose v2 configs.
9+
#
10+
# This would be better rewritten as a binary that talks directly to the docker
11+
# API, to avoid the brittle CLI parsing.
12+
docker inspect -f '{{ range .NetworkSettings.Networks }}{{ .NetworkID }}
13+
{{ end }}' $name > /tmp/current_networks
14+
15+
for network in `docker network ls | grep bridge | awk '{print $1}'`; do
16+
if ! grep -q $network /tmp/current_networks; then
17+
docker network connect $network $name
18+
fi
19+
done
20+
21+
# Now that we can reach these other hosts, reload nginx. Order is important
22+
# here, as nginx errors on startup if it can't resolve any of the specified
23+
# reverse proxy hosts.
24+
nginx -s reload

0 commit comments

Comments
 (0)