Thu, Aug 6, 2015
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:
docker-entrypoint.sh
.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 docker run
).
Here is what will happen when a container from this image runs:
/templates
directory is copied to /etc/nginx
.<FILENAME>.j2
(the extension of Jinja2 templates) inside /etc/nginx
are found (recursively) and evaluated, leaving them as <FILENAME>
. For example, /etc/nginx/sites-enabled/mysite.com.j2
would be stored as /etc/nginx/sites-enabled/mysite.com
..sh
scripts present in /docker-entrypoint-init.d
directory (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.
© 2024. All rights reserved.