Skip to content

Commit

Permalink
Add sites-enabled ENV variable interpolation
Browse files Browse the repository at this point in the history
All credit to shepmaster/nginx-template-image for the implementation
  • Loading branch information
abevoelker committed Feb 1, 2015
1 parent 7c1ca3a commit b87d6ba
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Docker image for nginx. Both [mainline and stable][mainline-vs-stable] releases

Differences from the [official Docker image][official-image]:

* Enables environment variable interpolation in config files (thanks to scripts taken from [`shepmaster/nginx-template-image`][nginx-template-image])
* Provides a *stable* tag (the official Docker nginx image only provides the mainline/development version of nginx).
* Uses the Ubuntu PPA installation path, so there are some extra compiled modules available.
* Sets `worker_processes` to `auto`. This value should typically be set to the number of cores on the machine. Because the Debian/Ubuntu installers set this at install-time to a static value equal to the detected number of cores on the machine, many Docker images get this wrong. `auto` means nginx will attempt to detect the number of cores when nginx starts up.
Expand Down Expand Up @@ -62,6 +63,8 @@ example.com
$ docker run -v /tmp/sites-enabled:/data/sites-enabled:ro abevoelker/nginx
```

If you need environment variable interpolation in your site configs, put the files in `/data/sites-templates` with a `.tmpl` extension and they will be copied to `/data/sites-enabled/` with the `.tmpl` extension removed. See [`shepmaster/nginx-template-image`][nginx-template-image] for more info.

### nginx.conf

If you have a custom `nginx.conf`, just mount it to `/data/conf/nginx.conf`:
Expand Down Expand Up @@ -102,3 +105,4 @@ MIT license.

[mainline-vs-stable]: http://nginx.com/blog/nginx-1-6-1-7-released/
[official-image]: https://github.com/nginxinc/docker-nginx
[nginx-template-image]: https://github.com/shepmaster/nginx-template-image
7 changes: 7 additions & 0 deletions mainline/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ MAINTAINER Abe Voelker <[email protected]>
# Provide a custom nginx.conf, tweaked for Docker use
COPY nginx.conf /data/conf/

# Add nginx templating helper scripts
COPY bin/ /usr/sbin/

# Ensure UTF-8 locale
COPY locale /etc/default/locale
RUN \
Expand All @@ -20,13 +23,17 @@ RUN \
DEBIAN_FRONTEND=noninteractive apt-get install -y nginx &&\
# Copy default config files to /data
/bin/bash -c "cp -a /etc/nginx/{conf.d,sites-enabled} /data/" &&\
# Create /data/sites-templates directory
mkdir /data/sites-templates &&\
chmod 0755 /data/sites-templates &&\
# Clean up APT and temporary files when done
apt-get clean &&\
DEBIAN_FRONTEND=noninteractive apt-get remove --purge -y software-properties-common &&\
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

VOLUME ["/var/cache/nginx", "/var/log/nginx"]

ENTRYPOINT ["entrypoint.sh"]
CMD ["nginx", "-c", "/data/conf/nginx.conf"]

EXPOSE 80 443
6 changes: 6 additions & 0 deletions mainline/bin/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -eu

render-templates.sh /data/sites-templates /data/sites-enabled
exec $@
28 changes: 28 additions & 0 deletions mainline/bin/render-templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -eu

indir="${1}"
outdir="${2}"

function template_files() {
find "${indir}" \
-mindepth 1 \
-maxdepth 1 \
-name '*.tmpl' \
-print0
}

function non_template_files() {
find "${indir}" \
-mindepth 1 \
-maxdepth 1 \
-not \
-name '*.tmpl' \
-print0
}

rm -rf "${outdir}"
mkdir -p "${outdir}"
template_files | xargs -0 substitute-env-vars.sh "${outdir}"
non_template_files | xargs -0 -I{} ln -s {} "${outdir}"
20 changes: 20 additions & 0 deletions mainline/bin/substitute-env-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -eu

output_dir=$1
shift
files=$@

function fill_in() {
perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : "\${$1}"/eg' "${1}"
}

function output_filename {
local destname=$(basename "${1}" '.tmpl')
echo "${output_dir}/${destname}"
}

for file in $files; do
fill_in "${file}" > $(output_filename "${file}")
done
7 changes: 7 additions & 0 deletions stable/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ MAINTAINER Abe Voelker <[email protected]>
# Provide a custom nginx.conf, tweaked for Docker use
COPY nginx.conf /data/conf/

# Add nginx templating helper scripts
COPY bin/ /usr/sbin/

# Ensure UTF-8 locale
COPY locale /etc/default/locale
RUN \
Expand All @@ -20,13 +23,17 @@ RUN \
DEBIAN_FRONTEND=noninteractive apt-get install -y nginx &&\
# Copy default config files to /data
/bin/bash -c "cp -a /etc/nginx/{conf.d,sites-enabled} /data/" &&\
# Create /data/sites-templates directory
mkdir /data/sites-templates &&\
chmod 0755 /data/sites-templates &&\
# Clean up APT and temporary files when done
apt-get clean &&\
DEBIAN_FRONTEND=noninteractive apt-get remove --purge -y software-properties-common &&\
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

VOLUME ["/var/cache/nginx", "/var/log/nginx"]

ENTRYPOINT ["entrypoint.sh"]
CMD ["nginx", "-c", "/data/conf/nginx.conf"]

EXPOSE 80 443
6 changes: 6 additions & 0 deletions stable/bin/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -eu

render-templates.sh /data/sites-templates /data/sites-enabled
exec $@
28 changes: 28 additions & 0 deletions stable/bin/render-templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -eu

indir="${1}"
outdir="${2}"

function template_files() {
find "${indir}" \
-mindepth 1 \
-maxdepth 1 \
-name '*.tmpl' \
-print0
}

function non_template_files() {
find "${indir}" \
-mindepth 1 \
-maxdepth 1 \
-not \
-name '*.tmpl' \
-print0
}

rm -rf "${outdir}"
mkdir -p "${outdir}"
template_files | xargs -0 substitute-env-vars.sh "${outdir}"
non_template_files | xargs -0 -I{} ln -s {} "${outdir}"
20 changes: 20 additions & 0 deletions stable/bin/substitute-env-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -eu

output_dir=$1
shift
files=$@

function fill_in() {
perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : "\${$1}"/eg' "${1}"
}

function output_filename {
local destname=$(basename "${1}" '.tmpl')
echo "${output_dir}/${destname}"
}

for file in $files; do
fill_in "${file}" > $(output_filename "${file}")
done

0 comments on commit b87d6ba

Please sign in to comment.