├── Dockerfile ├── README.md ├── consul-template.service ├── nginx.conf └── nginx.service /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM yanana/phusion-nginx 2 | 3 | MAINTAINER Shun Yanaura 4 | 5 | ENV DL_URL https://github.com/hashicorp/consul-template/releases/download/v0.7.0/consul-template_0.7.0_linux_amd64.tar.gz 6 | RUN curl -sSL $DL_URL | tar -C /usr/local/bin --strip-components 1 -zxf - 7 | 8 | ADD nginx.service /etc/service/nginx/run 9 | ADD consul-template.service /etc/service/consul-template/run 10 | RUN find /etc/service -type f -name 'run' -a ! -executable -exec chmod +x {} \; 11 | 12 | RUN rm -v /etc/nginx/conf.d/* 13 | ADD nginx.conf /etc/nginx/nginx.conf 14 | 15 | VOLUME /etc/consul-templates 16 | 17 | CMD ["my_init"] 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-nginx-consul-template 2 | 3 | [Consul Template](https://github.com/hashicorp/consul-template) powered Nginx container. 4 | 5 | ## Usage 6 | 7 | This container is not intended to be used by itself. You have to create a container which inherits this. E.g., with a configuration template app.conf.ctmpl Dockerfile would be: 8 | 9 | ``` 10 | FROM yanana/nginx-consul-template 11 | 12 | ADD app.conf.ctmpl /etc/consul-templates/app.conf.ctmpl 13 | 14 | CMD ["my_init"] 15 | ``` 16 | 17 | And the template: 18 | 19 | ``` 20 | upstream app { 21 | least_conn; 22 | {{range service "some-cool-app"}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1; 23 | {{else}}server 127.0.0.1:65535; # force a 502{{end}} 24 | } 25 | ``` 26 | 27 | And given running [docker-consul](https://github.com/progrium/docker-consul) container named 'consul': 28 | 29 | ```sh 30 | docker run -d \ 31 | -v /somewhere/to/put/logs:/var/log/nginx:rw \ 32 | --link consul:consul \ 33 | -p port-to-publish-nginx:80 \ 34 | --name container-name \ 35 | -e "SERVICE_NAME=service-name-to-register-to-consul" \ 36 | -e "SERVICE_TAGS=tag1,tag2,tag3" \ 37 | yanana/nginx-consul-template 38 | ``` 39 | 40 | That's all :sushi: 41 | -------------------------------------------------------------------------------- /consul-template.service: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | exec consul-template \ 4 | -consul consul:8500 \ 5 | -template "/etc/consul-templates/app.conf.ctmpl:/etc/nginx/conf.d/app.conf:sv hup nginx || true" 6 | -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes auto; 3 | worker_rlimit_nofile 100000; 4 | 5 | error_log /var/log/nginx/error.log; 6 | 7 | pid /var/run/nginx.pid; 8 | 9 | events { 10 | worker_connections 8192; 11 | multi_accept on; 12 | use epoll; 13 | } 14 | 15 | http { 16 | include /etc/nginx/mime.types; 17 | default_type application/octet-stream; 18 | 19 | log_format ltsv 'time:$time_iso8601\t' 20 | 'remote_addr:$remote_addr\t' 21 | 'request_method:$request_method\t' 22 | 'request_length:$request_length\t' 23 | 'request_uri:$request_uri\t' 24 | 'https:$https\t' 25 | 'uri:$uri\t' 26 | 'query_string:$query_string\t' 27 | 'status:$status\t' 28 | 'bytes_sent:$bytes_sent\t' 29 | 'body_bytes_sent:$body_bytes_sent\t' 30 | 'referer:$http_referer\t' 31 | 'useragent:$http_user_agent\t' 32 | 'forwardedfor:$http_x_forwarded_for\t' 33 | 'request_time:$request_time\t' 34 | 'upstream_response_time:$upstream_response_time'; 35 | 36 | include /etc/nginx/conf.d/*.conf; 37 | 38 | access_log /var/log/nginx/access.log ltsv; 39 | 40 | sendfile on; 41 | tcp_nopush on; 42 | 43 | keepalive_timeout 120; 44 | 45 | gzip on; 46 | gzip_buffers 16 8k; 47 | gzip_comp_level 4; 48 | gzip_types application/json; 49 | gzip_vary on; 50 | gzip_proxied off; 51 | 52 | server_tokens off; 53 | 54 | index index.html index.htm; 55 | 56 | server { 57 | listen 80; 58 | server_name "localhost"; 59 | root /usr/share/nginx/html; 60 | 61 | location / { 62 | rewrite ^/(.+) $1 break; 63 | proxy_pass http://app/$1$is_args$args; 64 | proxy_set_header Host $host; 65 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 66 | proxy_set_header X-Real-IP $remote_addr; 67 | proxy_redirect off; 68 | 69 | client_max_body_size 10m; 70 | client_body_buffer_size 128k; 71 | 72 | proxy_connect_timeout 90; 73 | proxy_send_timeout 90; 74 | 75 | proxy_buffer_size 4k; 76 | proxy_buffers 4 32k; 77 | proxy_busy_buffers_size 64k; 78 | } 79 | 80 | location /nginx_status { 81 | stub_status on; 82 | access_log off; 83 | allow 127.0.0.1; 84 | deny all; 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /nginx.service: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | /usr/sbin/nginx -c /etc/nginx/nginx.conf -t && \ 4 | exec /usr/sbin/nginx -c /etc/nginx/nginx.conf -g "daemon off;" 5 | --------------------------------------------------------------------------------