Skip to content

Commit 1c98df2

Browse files
committed
Merge pull request nginx-proxy#337 from baptistedonaux/master
Support Overlay Network
2 parents a72c7e6 + f669345 commit 1c98df2

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ update-dependencies:
77
docker pull python:3
88
docker pull rancher/socat-docker:latest
99
docker pull appropriate/curl:latest
10-
docker pull docker:1.9
10+
docker pull docker:1.10
1111

1212
test:
1313
docker build -t jwilder/nginx-proxy:bats .

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ If you need to support multiple virtual hosts for a container, you can separate
3939

4040
You can also use wildcards at the beginning and the end of host name, like `*.bar.com` or `foo.bar.*`. Or even a regular expression, which can be very useful in conjunction with a wildcard DNS service like [xip.io](http://xip.io), using `~^foo\.bar\..*\.xip\.io` will match `foo.bar.127.0.0.1.xip.io`, `foo.bar.10.0.2.2.xip.io` and all other given IPs. More information about this topic can be found in the nginx documentation about [`server_names`](http://nginx.org/en/docs/http/server_names.html).
4141

42+
### Multiple Networks
43+
44+
With the addition of [overlay networking](https://docs.docker.com/engine/userguide/networking/get-started-overlay/) in Docker 1.9, your `nginx-proxy` container may need to connect to backend containers on multiple networks. By default, if you don't pass the `--net` flag when your `nginx-proxy` container is created, it will only be attached to the default `bridge` network. This means that it will not be able to connect to containers on networks other than `bridge`.
45+
46+
If you want your `nginx-proxy` container to be attached to a different network, you must pass the `--net=my-network` option in your `docker create` or `docker run` command. At the time of this writing, only a single network can be specified at container creation time. To attach to other networks, you can use the `docker network connect` command after your container is created:
47+
48+
```console
49+
$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro \
50+
--name my-nginx-proxy --net my-network jwilder/nginx-proxy
51+
$ docker network connect my-other-network my-nginx-proxy
52+
```
53+
54+
In this example, the `my-nginx-proxy` container will be connected to `my-network` and `my-other-network` and will be able to proxy to other containers attached to those networks.
55+
4256
### SSL Backends
4357

4458
If you would like to connect to your backend using HTTPS instead of HTTP, set `VIRTUAL_PROTO=https` on the backend container.

circle.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
machine:
22
pre:
3-
- sudo curl -L -o /usr/bin/docker 'https://s3-external-1.amazonaws.com/circle-downloads/docker-1.9.1-circleci'
4-
- sudo chmod 0755 /usr/bin/docker
3+
- curl -sSL https://s3.amazonaws.com/circle-downloads/install-circleci-docker.sh | bash -s -- 1.10.0
54
services:
65
- docker
76

nginx.tmpl

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
2+
13
{{ define "upstream" }}
24
{{ if .Address }}
35
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
46
{{ if and .Container.Node.ID .Address.HostPort }}
57
# {{ .Container.Node.Name }}/{{ .Container.Name }}
68
server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
79
{{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
8-
{{ else }}
10+
{{ else if .Network }}
911
# {{ .Container.Name }}
10-
server {{ .Address.IP }}:{{ .Address.Port }};
12+
server {{ .Network.IP }}:{{ .Address.Port }};
1113
{{ end }}
12-
{{ else }}
14+
{{ else if .Network }}
1315
# {{ .Container.Name }}
14-
server {{ .Container.IP }} down;
16+
server {{ .Network.IP }} down;
1517
{{ end }}
1618
{{ end }}
1719

@@ -75,15 +77,24 @@ server {
7577
upstream {{ $host }} {
7678
{{ range $container := $containers }}
7779
{{ $addrLen := len $container.Addresses }}
78-
{{/* If only 1 port exposed, use that */}}
79-
{{ if eq $addrLen 1 }}
80-
{{ $address := index $container.Addresses 0 }}
81-
{{ template "upstream" (dict "Container" $container "Address" $address) }}
82-
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
83-
{{ else }}
84-
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
85-
{{ $address := where $container.Addresses "Port" $port | first }}
86-
{{ template "upstream" (dict "Container" $container "Address" $address) }}
80+
81+
{{ range $knownNetwork := $CurrentContainer.Networks }}
82+
{{ range $containerNetwork := $container.Networks }}
83+
{{ if eq $knownNetwork.Name $containerNetwork.Name }}
84+
## Can be connect with "{{ $containerNetwork.Name }}" network
85+
86+
{{/* If only 1 port exposed, use that */}}
87+
{{ if eq $addrLen 1 }}
88+
{{ $address := index $container.Addresses 0 }}
89+
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
90+
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
91+
{{ else }}
92+
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
93+
{{ $address := where $container.Addresses "Port" $port | first }}
94+
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
95+
{{ end }}
96+
{{ end }}
97+
{{ end }}
8798
{{ end }}
8899
{{ end }}
89100
}

test/docker.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ load test_helpers
5454
@test "[$TEST_FILE] separated containers (nginx + docker-gen + nginx.tmpl)" {
5555
docker_clean bats-nginx
5656
docker_clean bats-docker-gen
57-
57+
5858
# GIVEN a simple nginx container
5959
run docker run -d \
6060
--label bats-type="nginx" \
@@ -73,6 +73,7 @@ load test_helpers
7373
-v /var/run/docker.sock:/tmp/docker.sock:ro \
7474
-v $BATS_TEST_DIRNAME/../nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro \
7575
--volumes-from bats-nginx \
76+
--expose 80 \
7677
jwilder/docker-gen:0.7.0 \
7778
-notify-sighup bats-nginx \
7879
-watch \
@@ -91,7 +92,7 @@ load test_helpers
9192
docker logs bats-docker-gen
9293
false
9394
} >&2
94-
95+
9596
# THEN
9697
assert_nginxproxy_behaves bats-nginx
9798
}
@@ -120,4 +121,3 @@ function assert_nginxproxy_behaves {
120121
run curl_container $container /data --header "Host: webFOO.bats" --head
121122
assert_output -l 0 $'HTTP/1.1 503 Service Temporarily Unavailable\r'
122123
}
123-

test/lib/docker_helpers.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ function docker_tcp {
6262
--expose 2375 \
6363
-v /var/run/docker.sock:/var/run/docker.sock \
6464
rancher/socat-docker
65-
docker run --label bats-type="docker" --link "$container_name:docker" docker:1.9 version
65+
docker run --label bats-type="docker" --link "$container_name:docker" docker:1.10 version
6666
}

0 commit comments

Comments
 (0)