Skip to content

Instantly share code, notes, and snippets.

@misebox
Last active July 9, 2021 17:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misebox/c7b710e74c5a30809ff79724a3f8e816 to your computer and use it in GitHub Desktop.
Save misebox/c7b710e74c5a30809ff79724a3f8e816 to your computer and use it in GitHub Desktop.
Script for using environment variables in /etc/nginx/**/*.conf.template
#!/bin/sh
# Create /etc/nginx/**/*.conf from replacing the strings like
# ${NGINX_xxxxx} contained in /etc/nginx/**/*.conf.template
# with the value of each environment variable.
envs="$(env | egrep '^NGINX_' | sed -r 's/^(.+)=.*/\$\$\1/g')"
for template in `find /etc/nginx -name '*.conf.template'`
do
conf=`basename -s .template $template`
envsubst "$envs" \
< "$template" \
> "$conf"
done
@misebox
Copy link
Author

misebox commented Jul 9, 2021

How to use with docker-compose:

services:
  nginx:
    image: nginx:1.21-alpine
    volumes:    
      - ./nginx/envsubst-nginx-conf:/usr/local/bin/envsubst-nginx-conf:ro
      - ./nginx/nginx.conf.template:/etc/nginx/nginx.conf.template:ro
      - ./nginx/conf.d/www-sample-com.conf.template:ro
    ports:
      - "8080:80"
    environment:
      NGINX_SERVER_NAME: www.sample.com
      NGINX_PORT: 80
    command: sh -c "envsubst-nginx-conf && nginx -g 'daemon off;'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment