From e3cca7496df5bbc093fdaabc128749a288053371 Mon Sep 17 00:00:00 2001 From: Syunsuke Komma Date: Tue, 30 Apr 2019 21:23:48 +0900 Subject: [PATCH 1/3] add usage of nginx-envsubst --- nginx/content.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/nginx/content.md b/nginx/content.md index 8d8a26003..df6ca3000 100644 --- a/nginx/content.md +++ b/nginx/content.md @@ -68,7 +68,7 @@ $ docker run --name my-custom-nginx-container -d custom-nginx ### Using environment variables in %%IMAGE%% configuration -Out-of-the-box, %%IMAGE%% doesn't support environment variables inside most configuration blocks. But `envsubst` may be used as a workaround if you need to generate your %%IMAGE%% configuration dynamically before %%IMAGE%% starts. +Out-of-the-box, %%IMAGE%% doesn't support environment variables inside most configuration blocks. But this image contains a script to run `envsubst`, named `nginx-envsubst`. The script will be invoked automatically and will extract environment variables before %%IMAGE%% starts. Here is an example using docker-compose.yml: @@ -76,19 +76,33 @@ Here is an example using docker-compose.yml: web: image: %%IMAGE%% volumes: - - ./mysite.template:/etc/nginx/conf.d/mysite.template + - ./default.conf.template:/etc/nginx/templates/default.conf.template ports: - "8080:80" environment: - NGINX_HOST=foobar.com - NGINX_PORT=80 - command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'" ``` -The `mysite.template` file may then contain variable references like this: +The `default.conf.template` file may then contain variable references like this: -`listen ${NGINX_PORT}; -` +``` +listen ${NGINX_PORT}; +``` + +By default, `nginx-envsubst` reads template files in `/etc/nginx/templates` directory and outputs the result of execution using `envsubst` to `/etc/nginx/conf.d`. + +This behavior can be changed via the following environent variables: + +- `NGINX_ENVSUBST_TEMPLATE_DIR` + - A directory containing template files (default: `/etc/nginx/templates`) +- `NGINX_ENVSUBST_TEMPLATE_SUFFIX` + - A suffix of template files (default: `.template`) + - `nginx-envsubst` only processes the files whose name contains this suffix. +- `NGINX_ENVSUBST_OUTPUT_DIR` + - A directory where the result of executing envsubst is output (default: `/etc/nginx/conf.d`) + - The output filename is the template filename with the suffix removed. + - ex.) `/etc/nginx/templates/default.conf.template` will be output with the filename `/etc/nginx/conf.d/default.conf`. ## Running %%IMAGE%% in read-only mode From 1af6dc6cd3a825ca8b0e8d6f27dd8493203c290c Mon Sep 17 00:00:00 2001 From: Syunsuke Komma Date: Tue, 30 Apr 2019 22:12:11 +0900 Subject: [PATCH 2/3] fix a bit --- nginx/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx/content.md b/nginx/content.md index df6ca3000..f43654258 100644 --- a/nginx/content.md +++ b/nginx/content.md @@ -90,7 +90,7 @@ The `default.conf.template` file may then contain variable references like this: listen ${NGINX_PORT}; ``` -By default, `nginx-envsubst` reads template files in `/etc/nginx/templates` directory and outputs the result of execution using `envsubst` to `/etc/nginx/conf.d`. +By default, `nginx-envsubst` reads template files in `/etc/nginx/templates` and outputs the result of executing `envsubst` to `/etc/nginx/conf.d`. This behavior can be changed via the following environent variables: From 7f2f4c34787efaf606c4bf8506ca0f1184b9121c Mon Sep 17 00:00:00 2001 From: Syunsuke Komma Date: Thu, 2 May 2019 08:21:05 +0900 Subject: [PATCH 3/3] modify the usage --- nginx/content.md | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/nginx/content.md b/nginx/content.md index f43654258..701bb287d 100644 --- a/nginx/content.md +++ b/nginx/content.md @@ -68,7 +68,7 @@ $ docker run --name my-custom-nginx-container -d custom-nginx ### Using environment variables in %%IMAGE%% configuration -Out-of-the-box, %%IMAGE%% doesn't support environment variables inside most configuration blocks. But this image contains a script to run `envsubst`, named `nginx-envsubst`. The script will be invoked automatically and will extract environment variables before %%IMAGE%% starts. +Out-of-the-box, %%IMAGE%% doesn't support environment variables inside most configuration blocks. But this image has a function, which will extract environment variables before %%IMAGE%% starts. Here is an example using docker-compose.yml: @@ -76,7 +76,7 @@ Here is an example using docker-compose.yml: web: image: %%IMAGE%% volumes: - - ./default.conf.template:/etc/nginx/templates/default.conf.template + - ./templates:/etc/nginx/templates ports: - "8080:80" environment: @@ -84,25 +84,29 @@ web: - NGINX_PORT=80 ``` -The `default.conf.template` file may then contain variable references like this: +By default, this function reads template files in `/etc/nginx/templates/*.template` and outputs the result of executing `envsubst` to `/etc/nginx/conf.d`. -``` -listen ${NGINX_PORT}; -``` +So if you place `templates/default.conf.template` file, which contains variable references like this: -By default, `nginx-envsubst` reads template files in `/etc/nginx/templates` and outputs the result of executing `envsubst` to `/etc/nginx/conf.d`. + listen ${NGINX_PORT}; -This behavior can be changed via the following environent variables: +outputs to `/etc/nginx/conf.d/default.conf` like this: -- `NGINX_ENVSUBST_TEMPLATE_DIR` - - A directory containing template files (default: `/etc/nginx/templates`) -- `NGINX_ENVSUBST_TEMPLATE_SUFFIX` - - A suffix of template files (default: `.template`) - - `nginx-envsubst` only processes the files whose name contains this suffix. -- `NGINX_ENVSUBST_OUTPUT_DIR` - - A directory where the result of executing envsubst is output (default: `/etc/nginx/conf.d`) - - The output filename is the template filename with the suffix removed. - - ex.) `/etc/nginx/templates/default.conf.template` will be output with the filename `/etc/nginx/conf.d/default.conf`. + listen 80; + +This behavior can be changed via the following environment variables: + +- `NGINX_ENVSUBST_TEMPLATE_DIR` + - A directory which contains template files (default: `/etc/nginx/templates`) + - When this directory doesn't exist, this function will do nothing about template processing. +- `NGINX_ENVSUBST_TEMPLATE_SUFFIX` + - A suffix of template files (default: `.template`) + - This function only processes the files whose name ends with this suffix. +- `NGINX_ENVSUBST_OUTPUT_DIR` + - A directory where the result of executing envsubst is output (default: `/etc/nginx/conf.d`) + - The output filename is the template filename with the suffix removed. + - ex.) `/etc/nginx/templates/default.conf.template` will be output with the filename `/etc/nginx/conf.d/default.conf`. + - This directory must be writable by the user running a container. ## Running %%IMAGE%% in read-only mode