In my last post about Docker, I outlined a technique I used for having a single container configurable via environment variables, even if the app we are trying to run does not support it by default. I used nginx as an example use case.
Today we are going one step further: what if we want to be kind and release said image to the public? Clearly, we have to generalize some steps of the process so we can save users from inheriting the Dockerfile.
We want an image that meets the following criteria:
Nginx config files will be generated from Jinja2 templates. The values of the variables are resolved from the environment variables (ie. those used by Docker and those passed by
Here is what will happen when a container from this image runs:
/templatesdirectory is copied to
<FILENAME>.j2(the extension of Jinja2 templates) inside
/etc/nginxare found (recursively) and evaluated, leaving them as
<FILENAME>. For example,
/etc/nginx/sites-enabled/mysite.com.j2would be stored as
.shscripts present in
/docker-entrypoint-init.ddirectory (if any) are executed.
The way we achieve all this is with a custom docker intialization script. You may view the sources on github.
The image is also available on the docker registry. It leverages automated builds, so it will get rebuilt whenever a change (such as version upgrade) is pushed to the official nginx image.