From 9748e03f327e7a4f59e3923e2385c2e9bd6438f1 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Thu, 1 Jun 2023 09:13:13 +0200 Subject: [PATCH] Emphazise the need for properly setup environment variables Some templates rely on environment variables to work properly. This is not really touched on in the documentation. Lets make this a little bit more obvious. --- README.md | 12 +++++++++--- templates/dnsmasq.hosts.conf.tmpl | 3 +++ templates/etcd.tmpl | 1 + templates/fluentd.conf.tmpl | 1 + templates/logrotate.tmpl | 4 ++++ templates/nginx.tmpl | 9 +++++++-- 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4027e11a..4725ce01 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,12 @@ $ docker run -d --name nginx-gen --volumes-from nginx \ -t nginxproxy/docker-gen -notify-sighup nginx -watch -only-exposed /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf ``` +Start a container, taking note of any Environment variables a container expects. See the top of a template for details. + +``` +$ docker run --env VIRTUAL_HOST='example.com' --env VIRTUAL_PORT=80 ... +``` + === ### Usage @@ -206,7 +212,7 @@ e75a60548dc9 = 1 # a key can be either container name (nginx) or ID ### Templating -The templates used by docker-gen are written using the Go [text/template](http://golang.org/pkg/text/template/) language. In addition to the [built-in functions](http://golang.org/pkg/text/template/#hdr-Functions) supplied by Go, docker-gen uses [sprig](https://masterminds.github.io/sprig/) and some additional functions to make it simpler (or possible) to generate your desired output. +The templates used by docker-gen are written using the Go [text/template](http://golang.org/pkg/text/template/) language. In addition to the [built-in functions](http://golang.org/pkg/text/template/#hdr-Functions) supplied by Go, docker-gen uses [sprig](https://masterminds.github.io/sprig/) and some additional functions to make it simpler (or possible) to generate your desired output. Some templates rely on environment variables within the container to make decisions on what to generate from the template. #### Emit Structure @@ -411,10 +417,10 @@ Start nginx-proxy: $ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock -t nginxproxy/nginx-proxy ``` -Then start containers with a VIRTUAL_HOST env variable: +Then start containers with a VIRTUAL_HOST (and the VIRTUAL_PORT if more than one port is exposed) env variable: ``` -$ docker run -e VIRTUAL_HOST=foo.bar.com -t ... +$ docker run -e VIRTUAL_HOST=foo.bar.com -e VIRTUAL_PORT=80 -t ... ``` If you wanted to run docker-gen directly on the host, you could do it with: diff --git a/templates/dnsmasq.hosts.conf.tmpl b/templates/dnsmasq.hosts.conf.tmpl index a8de9be0..12a8e18e 100644 --- a/templates/dnsmasq.hosts.conf.tmpl +++ b/templates/dnsmasq.hosts.conf.tmpl @@ -1,3 +1,6 @@ +{{/* Simple dnsmasq template generating host entries */}} +{{/* Domains are hard-coded, replace 'docker.comany.com' below */}} + {{$domain := "docker.company.com"}} {{range $key, $value := .}} # {{ $value.Name }} ({{$value.ID}} from {{$value.Image.Repository}}) diff --git a/templates/etcd.tmpl b/templates/etcd.tmpl index fce6ea21..9ccdf528 100644 --- a/templates/etcd.tmpl +++ b/templates/etcd.tmpl @@ -1,3 +1,4 @@ +{{/* etcd template to generate registration script */}} #!/bin/bash diff --git a/templates/fluentd.conf.tmpl b/templates/fluentd.conf.tmpl index 92149b27..9a666c46 100644 --- a/templates/fluentd.conf.tmpl +++ b/templates/fluentd.conf.tmpl @@ -1,3 +1,4 @@ +{{/* Generates fluentd configuration entries */}} ## File input ## read docker logs with tag=docker.container diff --git a/templates/logrotate.tmpl b/templates/logrotate.tmpl index fe2a5dd5..b1b6e7b9 100644 --- a/templates/logrotate.tmpl +++ b/templates/logrotate.tmpl @@ -1,3 +1,7 @@ +{{/* Generate logrotate snippets for logrotate based on files listed in */}} +{{/* the comma separated environment variable LOG_FILES */}} +{{/* e.g. docker run --env='/var/log/messages,/var/log/lastlog' ... */}} + {{ range $index, $value := $ }} {{ $logs := $value.Env.LOG_FILES }} {{ if $logs }} diff --git a/templates/nginx.tmpl b/templates/nginx.tmpl index f4aa32e0..58f6c77e 100644 --- a/templates/nginx.tmpl +++ b/templates/nginx.tmpl @@ -1,3 +1,8 @@ +{{/* default nginx configuration template */}} +{{/* Generate a configuration file based on the containers mandatory */}} +{{/* VIRTUAL_HOST environment variable and the exposed ports. If multiple */}} +{{/* ports are exposed, the first one is used, unless set with VIRTUAL_PORT */}} + server { listen 80 default_server; server_name _; # This is just an invalid value which will never trigger on a real hostname. @@ -13,7 +18,7 @@ upstream {{ $host }} { {{ $addrLen := len $value.Addresses }} {{ $network := index $value.Networks 0 }} - + {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} {{ with $address := index $value.Addresses 0 }} @@ -62,4 +67,4 @@ server { proxy_set_header Connection ""; } } -{{ end }} \ No newline at end of file +{{ end }}