Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Zwanenburg committed Jan 25, 2020
1 parent 3983f89 commit d7e7d21
Showing 1 changed file with 71 additions and 44 deletions.
115 changes: 71 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,70 +13,97 @@ container to be PID 1.
- Mustache templates
- Environment variables as source of data for your templates
- Glob support for specifying templates
- Chosing the rendered filename by search and replace of the template filename
- Executes the binary of your choice after rendering templates
- Errors out when rendering a template containing a reference to a non-existing
environment variable (can be disabled with the `--allow-missing` flag)

## `mustache-then-exec --help`

```
Replaces TEMPLATE files (which are Go globs) with their contents
rendered as a Mustache template, where the environment variables are
passed as data to those tempales.
-allow-missing
Whether to allow missing variables (default: false).
Usage: mustache-then-exec [--allow-missing] [--template TEMPLATE] [--glob-template GLOB] BINARY [ARG [ARG ...]]
Positional arguments:
BINARY the binary to run after rendering the templates
ARG arguments to the binary to run after rendering the templates
Options:
--allow-missing, -a whether to allow missing variables (default: false)
--template TEMPLATE, -t TEMPLATE
path to a template to be rendered
--glob-template GLOB, -g GLOB
glob for templates to be rendered
--help, -h display this help and exit
```

## Example

Here's a Dockerfile for a simple nginx proxy:
Here's a Dockerfile for a simple image that executes a `cat` binary:

```dockerfile
FROM nginx:alpine
FROM busybox

COPY mustache-then-exec /

RUN echo $'\
events { \n\
} \n\
http { \n\
server { \n\
listen 80; \n\
location / { \n\
proxy_pass {{{NGINX_PROXY_UPSTREAM}}}; \n\
} \n\
} \n\
}' > /etc/nginx/nginx.conf

ENTRYPOINT [ "/mustache-then-exec", "/etc/nginx/*.conf", "--", "/usr/sbin/nginx" ]

CMD [ "-g", "daemon off;" ]
RUN echo "Hello, {{{THING_TO_GREET}}}!" > /mytemplate

ENTRYPOINT [ "/mustache-then-exec", "-t", "/mytemplate", "--", "/bin/cat" ]
```

Which you can now run as follows (image name depending on how you tagged your
image during `docker build`):

```shellsession
$ docker run --rm -p 80:80 mustache-then-exec-nginx-proxy
Filling template: /etc/nginx/fastcgi.conf
Filling template: /etc/nginx/nginx.conf
Error: Missing variable "NGINX_PROXY_UPSTREAM"
$ docker run --rm -d -p 80:80 -e NGINX_PROXY_UPSTREAM=https://httpbin.org/get mustache-then-exec-nginx-proxy
f3a67fa793821c3815997cfcc78a6619a01836f3c61380606922c6bf779ab30d
$ docker exec f3a67fa793821c3815997cfcc78a6619a01836f3c61380606922c6bf779ab30d pstree -p
nginx(1)---nginx(11)
$ curl http://localhost:80/get
curl http://localhost:80
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.68.0",
},
"origin": "1.1.1.1",
"url": "https://httpbin.org/get"
}
$ docker kill f3a67fa793821c3815997cfcc78a6619a01836f3c61380606922c6bf779ab30d
$ #What happens when I don't set the required environment variable?
$ docker run --rm cat /mytemplate /mytemplate
Rendering template: /mytemplate; output: /mytemplate
Error: Missing variable "THING_TO_GREET"
$ #And what happens when I set it?
$ docker run --rm -e THING_TO_GREET=World cat /mytemplate
Rendering template: /mytemplate; output: /mytemplate
Hello, World!
$ #I'm goingo tm make cat read from stdin, so that it keeps running
$ docker run --rm -td -e THING_TO_GREET=World cat -
c6e4910c274d7b0984924c744d147935dbd62a89fe02da3a731a9264f794020c
$ #And now I can see what processes are running in my container: no mustache-then-exec!
$ docker exec c6e4910c274d7b0984924c744d147935dbd62a89fe02da3a731a9264f794020c pstree -p
cat(1)
$ docker kill c6e4910c274d7b0984924c744d147935dbd62a89fe02da3a731a9264f794020c
c6e4910c274d7b0984924c744d147935dbd62a89fe02da3a731a9264f794020c
```

## Example with renaming and globbing

Let's expand a little on the previous example, and add two extra features:

1. Globbing
1. Renaming with regexp search&replace

Another Dockerfile that does mostly the same:

```dockerfile
FROM busybox

COPY mustache-then-exec /

RUN echo "Hello, {{{THING_TO_GREET}}}!" | tee /mytemplate01 /mytemplate02

ENTRYPOINT [ "/mustache-then-exec", "-g", "/mytemplate*:mytemplate([0-9]+):replacement-filename-$1", "--", "/bin/cat" ]
```

And, assuming we also named this image `cat`, let's use it as follows:

```shellsession
$ #What happened to the templates?
$ docker run --rm -e THING_TO_GREET=World cat /mytemplate01 /mytemplate02
Rendering template: /mytemplate01; output: /replacement-filename-01
Rendering template: /mytemplate02; output: /replacement-filename-02
Hello, {{{THING_TO_GREET}}}!
Hello, {{{THING_TO_GREET}}}!
$ #And did we indeed get renamed files?
$ docker run --rm -e THING_TO_GREET=World cat /replacement-filename-01 /replacement-filename-02
Rendering template: /mytemplate01; output: /replacement-filename-01
Rendering template: /mytemplate02; output: /replacement-filename-02
Hello, World!
Hello, World!
```

0 comments on commit d7e7d21

Please sign in to comment.