Skip to content

Commit

Permalink
[WIP] Add TCP support
Browse files Browse the repository at this point in the history
  • Loading branch information
fchris82 committed Oct 11, 2017
1 parent f05f7a0 commit 33d80ea
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ RUN apt-get update \

# Configure Nginx and apply fix for very long server names
RUN echo "daemon off;" >> /etc/nginx/nginx.conf \
&& sed -i 's/worker_processes 1/worker_processes auto/' /etc/nginx/nginx.conf
&& sed -i 's/worker_processes 1/worker_processes auto/' /etc/nginx/nginx.conf \
&& echo "stream { \
include /etc/nginx/stream.conf.d/*.conf; \
}" >> /etc/nginx/nginx.conf \
&& mkdir -p /etc/nginx/stream.conf.d

# Install Forego
ADD https://github.com/jwilder/forego/releases/download/v0.16.1/forego /usr/local/bin/forego
RUN chmod u+x /usr/local/bin/forego

RUN apt-get update && apt-get install -y vim mysql-client && echo "127.0.0.1 whoami.loc" >> /etc/hosts

ENV DOCKER_GEN_VERSION 0.7.3

RUN wget https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
Expand All @@ -31,5 +37,8 @@ ENV DOCKER_HOST unix:///tmp/docker.sock

VOLUME ["/etc/nginx/certs", "/etc/nginx/dhparam"]

EXPOSE 80
EXPOSE 81

ENTRYPOINT ["/app/docker-entrypoint.sh"]
CMD ["forego", "start", "-r"]
4 changes: 2 additions & 2 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dockergen: docker-gen -watch -notify "nginx -s reload" /app/nginx.tmpl /etc/nginx/conf.d/default.conf
nginx: nginx
dockergen: docker-gen -config /app/docker-gen.cfg
nginx: nginx-debug
16 changes: 12 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
build: .
ports:
- "80:80"
- "180:80"
- "81:81"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro

whoami:
image: jwilder/whoami
environment:
- VIRTUAL_HOST=whoami.local
- VIRTUAL_HOST=whoami.loc

mysql:
image: mysql:5.6
environment:
VIRTUAL_TCP_HOST: whoami.loc
VIRTUAL_TCP_PORT: 3306
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
11 changes: 11 additions & 0 deletions docker-gen.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[config]]
template = "/app/nginx.tmpl"
dest = "/etc/nginx/conf.d/default.conf"
notifycmd = "nginx-debug -s reload"
watch = true

[[config]]
template = "/app/nginx.stream.tmpl"
dest = "/etc/nginx/stream.conf.d/default.conf"
notifycmd = "nginx-debug -s reload"
watch = true
67 changes: 67 additions & 0 deletions nginx.stream.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}

{{ define "upstream" }}
{{ if .Address }}
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
{{ if and .Container.Node.ID .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 if .Network }}
# {{ .Container.Name }}
server {{ .Network.IP }}:{{ .Address.Port }};
{{ end }}
{{ else if .Network }}
# {{ .Container.Name }}
server {{ .Network.IP }} down;
{{ end }}
{{ end }}

access_log off;
error_log /var/log/nginx/debug.log debug;

{{ if $.Env.RESOLVERS }}
resolver {{ $.Env.RESOLVERS }};
{{ end }}

{{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }}

{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_TCP_HOST" "," }}

{{ $host := trim $host }}
{{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }}

# {{ $host }}
upstream {{ $upstream_name }} {

{{ range $container := $containers }}
{{ $addrLen := len $container.Addresses }}

{{ range $knownNetwork := $CurrentContainer.Networks }}
{{ range $containerNetwork := $container.Networks }}
{{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
## Can be connect with "{{ $containerNetwork.Name }}" network

{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ $address := index $container.Addresses 0 }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_TCP_PORT env var, falling back to standard web port 81 */}}
{{ else }}
{{ $port := coalesce $container.Env.VIRTUAL_TCP_PORT "81" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
}

server {
listen {{ $host }}:81;
proxy_pass {{ trim $upstream_name }};
}

{{ end }}

0 comments on commit 33d80ea

Please sign in to comment.