From d7e7d2127234f6a40c4576e7dd43c42b7386fd0c Mon Sep 17 00:00:00 2001 From: Stefan Zwanenburg Date: Sat, 25 Jan 2020 05:01:09 +0100 Subject: [PATCH] Updated README --- README.md | 115 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index ff2d972..f8a8b0c 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ 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) @@ -20,63 +21,89 @@ container to be PID 1. ## `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! ```