From b4850173260c9f5f648955726ce62c9dc157e982 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Wed, 11 May 2022 11:08:22 +0200 Subject: feat: custom configuration file support via `USE_CUSTOM_CONFIG` env resolves #7 --- Dockerfile | 32 ++++++++++++++++++++++---------- README.md | 27 ++++++++++++++++++++++----- docker-entrypoint.sh | 26 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 15 deletions(-) create mode 100755 docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index b6729ed..860ef05 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,9 @@ ENV VERSION=${VERSION} # CGit ARG CGIT_VERSION=1.2.3-r2 ENV CGIT_VERSION=${CGIT_VERSION} -ENV CGIT_TITLE="cgit" + +# CGit default options +ENV CGIT_TITLE="CGit" ENV CGIT_DESC="The hyperfast web frontend for Git repositories" ENV CGIT_VROOT="/" ENV CGIT_SECTION_FROM_STARTPATH=0 @@ -35,19 +37,29 @@ RUN set -eux \ && true COPY cgit/cgit.conf /tmp/cgitrc.tmpl +COPY docker-entrypoint.sh / COPY nginx/nginx.conf /etc/nginx/nginx.conf COPY nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf -VOLUME [ "/srv/git", "/var/cache/cgit" ] +RUN set -eux \ + && echo "Creating application directories..." \ + && mkdir -p /var/cache/cgit \ + && mkdir -p /srv/git \ + && true + +RUN set -eux \ + && echo "Testing Nginx server configuration files..." \ + && nginx -c /etc/nginx/nginx.conf -t \ + && true + +ENTRYPOINT [ "/docker-entrypoint.sh" ] + +EXPOSE 80 + +STOPSIGNAL SIGQUIT + +CMD [ "nginx", "-g", "daemon off;" ] -CMD chown nginx:nginx /var/cache/cgit \ - && chmod u+g /var/cache/cgit \ - && envsubst < /tmp/cgitrc.tmpl > /etc/cgitrc \ - && spawn-fcgi \ - -u nginx -g nginx \ - -s /var/run/fcgiwrap.sock \ - -n -- /usr/bin/fcgiwrap \ - & nginx -g "daemon off;" # Metadata LABEL org.opencontainers.image.vendor="Jose Quintana" \ diff --git a/README.md b/README.md index f3992cd..6960ba4 100644 --- a/README.md +++ b/README.md @@ -39,12 +39,17 @@ docker run --rm -it \ FROM joseluisq/alpine-cgit ``` -## Volumes +## Key container paths -- `/srv/git`: Place for Git repositories. -- `/var/cache/cgit`: CGit caching of generated HTML. +- `/etc/cgitrc`: Default CGit configuration file. +- `/srv/git`: Default directory for Git repositories scanned by CGit. +- `/var/cache/cgit`: Default CGit caching directory of generated HTML. -## Settings via env variables +Note that all these paths can be overwritten via [Bind Mounts](https://docs.docker.com/storage/bind-mounts/) or [Docker Volumes](https://docs.docker.com/storage/volumes/). + +## Settings via environment variables + +CGit Docker image can be configured via environment variables. This is the default behaviour. - `CGIT_TITLE`: Website title. - `CGIT_DESC`: Website description. @@ -52,7 +57,19 @@ FROM joseluisq/alpine-cgit - `CGIT_SECTION_FROM_STARTPATH`: How many path elements from each repo path to use as a default section name. - `CGIT_MAX_REPO_COUNT`: Number of entries to list per page on the repository index page. -See default file configuration at [cgit/cgit.conf](./cgit/cgit.conf) +## Settings via custom configration file + +By default this Docker image will use a template file located at [cgit/cgit.conf](./cgit/cgit.conf) which is replaced with the env settings (mentioned above) at start up time. + +However if you want to use a custom `/etc/cgitrc` file then follow these steps: + +1. Provide the env variable `USE_CUSTOM_CONFIG=true` to prevent using the default config file. +2. Provide the custom config file as a [Bind Mount](https://docs.docker.com/storage/bind-mounts/) or [Docker Volume](https://docs.docker.com/storage/volumes/). For example `--volume my-config-file:/etc/cgitrc` +3. Provide the `cache-root` option in your config file. For example `cache-root=/var/cache/cgit` +4. Provide the `scan-path` option in your config file. For example `scan-path=/srv/git` +5. Provide the repositories folder as a [Bind Mount](https://docs.docker.com/storage/bind-mounts/) or [Docker Volume](https://docs.docker.com/storage/volumes/). For example `--volume my-repos:/srv/git` + +See [`cgitrc` man page](https://linux.die.net/man/5/cgitrc) for more detailed information. ## Contributions diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..2e875a4 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +set -e + +# Check for incomming Nginx server commands or subcommands only +if [ "$1" = "nginx" ] || [ "${1#-}" != "$1" ]; then + if [ "${1#-}" != "$1" ]; then + set -- nginx "$@" + fi + + chown nginx:nginx /var/cache/cgit + chmod u+g /var/cache/cgit + + # Replace environment variables only if `USE_CUSTOM_CONFIG` is not defined or equal to `false` + if [[ -z "$USE_CUSTOM_CONFIG" ]] || [[ "$USE_CUSTOM_CONFIG" = "false" ]]; then + envsubst < /tmp/cgitrc.tmpl > /etc/cgitrc + fi + + spawn-fcgi \ + -u nginx -g nginx \ + -s /var/run/fcgiwrap.sock \ + -n -- /usr/bin/fcgiwrap \ + & exec "$@" +else + exec "$@" +fi -- cgit v1.2.3