From c2fdda517575986115aff9a2b5bdfaac2da8039a Mon Sep 17 00:00:00 2001 From: salty Date: Wed, 13 Dec 2023 17:03:35 +0100 Subject: [PATCH] add config templating --- Dockerfile | 6 +++++- docker-entrypoint.sh | 36 ++++++++++++++++++++++++++++++++++++ haproxy.cfg | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100755 docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index afdc46f..422a418 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,4 +30,8 @@ ENV ALLOW_RESTARTS=0 \ TASKS=0 \ VERSION=1 \ VOLUMES=0 -COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg +RUN apk add --no-cache gettext +COPY docker-entrypoint.sh / +COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg.template + +ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..32be970 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,36 @@ +#!/bin/sh +set -e + +# Normalize the input for ENABLE_IPV6 to lowercase +ENABLE_IPV6_LOWER=$(echo "$ENABLE_IPV6" | tr '[:upper:]' '[:lower:]') + +# Check for different representations of 'true' and set BIND_CONFIG +case "$ENABLE_IPV6_LOWER" in + 1|true|yes) + BIND_CONFIG="[::]:2375 v4v6" + ;; + *) + BIND_CONFIG=":2375" + ;; +esac + +# Export BIND_CONFIG for envsubst +export BIND_CONFIG + +# Process the HAProxy configuration template +envsubst < /usr/local/etc/haproxy/haproxy.cfg.template > /usr/local/etc/haproxy/haproxy.cfg +cat /usr/local/etc/haproxy/haproxy.cfg + +# Command-line argument handling +if [ "${1#-}" != "$1" ]; then + set -- haproxy "$@" +fi + +if [ "$#" -eq 0 ] || [ "$1" = 'haproxy' ]; then + if [ "$1" = 'haproxy' ]; then + shift + fi + set -- haproxy -W -db -f /usr/local/etc/haproxy/haproxy.cfg "$@" +fi + +exec "$@" diff --git a/haproxy.cfg b/haproxy.cfg index c87c8d8..43e3526 100644 --- a/haproxy.cfg +++ b/haproxy.cfg @@ -44,7 +44,7 @@ backend docker-events timeout server 0 frontend dockerfrontend - bind :::2375 v4v6 + bind ${BIND_CONFIG} http-request deny unless METH_GET || { env(POST) -m bool } http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/((stop)|(restart)|(kill)) } { env(ALLOW_RESTARTS) -m bool } http-request allow if { path,url_dec -m reg -i ^(/v[\d\.]+)?/containers/[a-zA-Z0-9_.-]+/start } { env(ALLOW_START) -m bool }